简体   繁体   English

C ++ STL内存管理:堆栈还是堆?

[英]C++ STL Memory Management: Stack or Heap?

Normally when I use STL objects that are in a non-local scope I store pointers to the data I want to store. 通常,当我使用非本地范围内的STL对象时,我会存储指向要存储的数据的指针。 For instance 例如

std::vector<MyStruct*> 

When it's time to cleanup the vector I then go through and delete everything. 是时候清理向量了,然后检查并删除所有内容。 I've recently noticed that this isn't necessary like I thought it was. 我最近注意到,这不是我认为那样的必要。 For whatever reason I was thinking the STL classes stored data on the stack, whereas I now think it allocates it on the heap. 无论出于什么原因,我都在考虑STL类将数据存储在堆栈上,而现在我认为它是在堆上分配数据。 Is this correct? 这个对吗? Is the only real benefit to storing objects as pointers to reduce copy time? 将对象存储为指针以减少复制时间的唯一真正好处是?

The standard containers allocate memory via an Allocator object, whose type is passed as a template parameter. 标准容器通过Allocator对象分配内存,该对象的类型作为模板参数传递。 If you're not passing anything else, that'll be std::allocator<T> , which will use new to allocate the memory. 如果您没有传递其他任何内容,则将使用std::allocator<T> ,它将使用new来分配内存。

Bottom line: you can force them to allocate memory almost any way you want to, but by default it'll come from the free store. 底线:您可以强迫他们几乎以任何您想要的方式来分配内存,但是默认情况下它将来自免费存储。

If you really want a container of pointers were the container will own the pointee objects, (eg, will automatically delete them when the object is destroyed), you might want to look at Boost Pointer Containers . 如果您真的想要一个指针容器,那么该容器将拥有pointee对象(例如,在销毁对象时将自动删除它们),则可能需要查看Boost Pointer Containers

Using pointers to reduce copy time is a real benefit. 使用指针减少复制时间真正的好处。 Think of all the vector manipulations that can be improved by that - like a sorting, for example. 考虑一下所有可以改进的向量操作-例如排序。

Another real benefit (as noted in a comment above) is that this allows you to use polymorphism and store related objects in the same vector. 另一个真正的好处(如上面的评论中所述)是,这允许您使用多态并将相关对象存储在同一向量中。 Something you cannot do with scalar objects (non-pointers). 标量对象(非指针)无法执行的操作。

Whether you store data on stack or heap doesn't make a difference to how expensive it is to move that object (well... it does, but often quite negligible and not relevant in this discussion). 无论是将数据存储在堆栈还是堆上,都不会改变移动该对象的代价(好吧……确实如此,但在本次讨论中通常可以忽略不计且无关紧要)。

When you store pointers to your objects in an STL vector the vector does not take ownership of your objects. 当您将指向对象的指针存储在STL向量中时,向量不会获得对象的所有权 You still need to do due diligence and clean them up when they are not needed anymore. 您仍然需要进行尽职调查,并在不再需要它们时进行清理。

[...] whereas I now think it allocates it on the heap. [...]而我现在认为它是在堆上分配它的。 Is this correct? 这个对吗?

Yes. 是。 If you declare the vector as: 如果将向量声明为:

std::vector<MyStruct*> v;

then you're basically storing pointers in the vector, so the vector will allocate memory to store the pointers , not the objects pointed to by the pointers. 那么您基本上是将指针存储在向量中,因此向量将分配存储空间来存储指针 ,而不是指针所指向的对象。 So when the destructor runs, the vector will deallocate memory which it has allocated, it will not deallocate memory of the pointers themseves, ie it will not deallocate the memory pointed to by the pointers stored in the vector. 因此,当析构函数运行时,向量将释放其已分配的内存,而不将其本身分配的指针的内存释放,即,将不会释放向量中存储的指针指向的内存。

However, if you declare this: 但是,如果您声明:

std::vector<MyStruct> v;

then you're storing objects themselves, so the vector will allocate memory to store the objects, and it will deallocate it when the destructor runs. 那么您将自己存储对象,因此矢量将分配内存来存储对象,并且在析构函数运行时将对其进行分配。

When it's time to cleanup the vector I then go through and delete everything. 是时候清理向量了,然后检查并删除所有内容。 I've recently noticed that this isn't necessary like I thought it was. 我最近注意到,这不是我认为那样的必要。

Don't assume that. 不要以为那样。 If the pointers in your vector point to dynamically allocated memory then you WILL need to delete that memory, as the vector will not do it for you. 如果向量中的指针指向动态分配的内存,则您需要删除该内存,因为向量不会为您完成此操作。

For example if your code is of the type 例如,如果您的代码属于

 MyStruct* pNewStruct = new MyStruct;
 myVector.push_back(pNewStruct);

 ...
 ...

 myVector.clear();

you have a memory leak as you have not specifically deleted the memory you allocate to each element you add to the vector. 您存在内存泄漏,因为您没有明确删除分配给添加到向量中每个元素的内存。 The vector frees the memory it allocates within itself as part of being a dynamic array, but this just frees the array of pointers, NOT the memory that they point to 向量释放它作为动态数组的一部分在其内部分配的内存,但这只会释放指针数组,而不是它们指向的内存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM