开发者

Will destructor be called?

开发者 https://www.devze.com 2023-01-02 12:21 出处:网络
If I create a vector of vector of vector, if I clear the first vector, or the first vector开发者_Go百科 gets deleted, will all the child vectors call the destructor and free the memory or will it caus

If I create a vector of vector of vector, if I clear the first vector, or the first vector开发者_Go百科 gets deleted, will all the child vectors call the destructor and free the memory or will it cause a memory leak? Thanks


If you have:

vector <vector <vector <int> > > > v;
v.clear();

then destructors will be called suitably for all the subvectors.


There will only be a memory leak if you used new to create the contained vectors. Calling clear() on a vector will NOT call delete on the contained items.


The STL offers only value-semantics. This means that you shouldn't bother with memory allocation/deallocation issues as long as you don't put pointers into your containers. Objects are destructed when deleted from the container, so also when the container itself is destructed (or cleared).

This also means that many operations on those containers will involve (default) constucting, copying, and destructing objects.


Yes. Destructor will be called and the memory will be freed.

0

精彩评论

暂无评论...
验证码 换一张
取 消