vectors_shared_ptr [英] Vectors of shared_ptr

查看:97
本文介绍了vectors_shared_ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

typedef vector< boost::shared_ptr< CElement > > SharedList;
int size = 12;
SharedList list = SharedList( size );



超出范围时,向量会被销毁吗?是否有时需要打电话给clear()?

如果我不希望重新分配,在这种情况下是否仍需要调用reserve()?

[上面的代码]是否仅用空的[shared]指针填充向量?似乎是一种不好的做法,我错了吗?

第一次

谢谢,超人.

我确实了解向量如何与原始指针一起工作.因此,真正的问题是;当使用智能指针时,指针应该自动删除自身,在某些特殊情况下矢量可能不会被删除吗?我可以依靠向量超出范围"来删除智能指针吗?

唯一可能的泄漏是循环参考吗?

而且关于储备金的问题也没有得到回答.是我不需要在上面的代码示例中使用reserve()并且仍然避免重新分配吗?

第二次

谢谢彼得.



Will the vector be destroyed when it goes out of scope? Are there times when I''d be required to call clear()?

Would I still have to call reserve() in this case if I don''t want reallocations?

Does [the above code] just fill the vector with null [shared] pointers? Seems like a bad practice, am I wrong?

1st

Thanks, Superman.

I do understand how vectors work with raw pointers. So the real question is; When using smart pointers, where the pointers should automatically delete themselves, are there special circumstances with vectors where they might not get deleted? Can I rely on "vector going out of scope" to delete the smart pointers?

Is the only possible leak a cyclic reference?

And also the question about reserve wasn''t answered. Is it true that I wouldn''t need to use reserve() in the code example above and still avoid re-allocations?

2nd

Thanks, peterchen. Really learned a lot there.

推荐答案

vector超出范围时,它将被销毁,这意味着分配给vector的内存将被销毁.
但是,如果需要单独销毁其内容,则必须在vector超出范围之前手动完成.

例如,如果您有一个指针vector,并且这些指针是动态分配的内存,则当vector超出范围时,只会自动销毁分配用于存储指针的空间.因此,必须在vector超出作用域之前对其进行迭代,以破坏动态分配的指针.

reserve()最初将为vector分配一些最小空间.每当需要更多空间时,重新分配仍然会发生.

是的,在调用reserve()之后,分配的空间最初将包含零或NULL,具体取决于内容类型.
A vector will be destroyed when it goes out of scope, meaning the memory allocated for the vector will be destroyed.
But if its contents need to be destroyed independently, it has to be done manually before the vector goes out of scope.

For example, if you have a vector of pointers and these pointers are allocated memory dynamically, only the space allocated for storing the pointers will be destroyed automatically when the vector goes out of scope. The dynamically allocated pointers therefore must be destroyed by iterating through the vector before it goes out of scope.

reserve() will initially allocate some minimum space to the vector. Reallocations will still occur whenever more space is needed.

Yes, after calling reserve(), the allocated space will initially contain zeros or NULLs depending on the content type.


1.是的,载体将被销毁.如果向量保留对它们的最后引用,则CElement将被销毁.

2.循环引用是发生泄漏的唯一可能性(除非您操纵指针的实现细节,例如人为地人工不支持地增加引用计数)

3.当您违反曾经共享的规则时,您可能会访问一个已经被销毁的元素:

1. Yes, the vector will be destroyed. CElement''s will be destroyed if the vector held the last reference to them.

2. Circular reference is the only possibility for a leak (unless you manipulate the implementation details of the pointer, e.g. artificially manually unsupportedly icnreasing the reference count)

3. You might access an already-destroyed element when you violate the once-shared-always-shared rule:

boost__shared_ptr<a> aptr(new a);
a * pa = aptr.get();
boost::shared_ptr<a> aptr2(pa);  // ERROR: creating separate shared_ptr to same object
aptr2.reset(); // releaseing one, FATAL: aptr is stale!



请注意,使用intrusive_ptr可以.

4.是的,矢量的规则相同:如果调用resize(或调整大小的构造函数),将附加默认的构造元素.保留并保留内存而无需构造元素.

请注意,没有储备金的情况下,重新分配仍然会发生-但至少副本是便宜的.



Note that this is OK with intrusive_ptr.

4. Yes, the same rules for vector apply: if you call resize (or the sizing constructor), default constructed elements will be appended. reserve with reserve the memorys without constructing the elements.

Note that without reserve, the reallocations still happen - but at least the copies are cheap.


这篇关于vectors_shared_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆