简体   繁体   English

C ++:指针元素的向量是否会在销毁时自动释放每个指针所指向的动态内存?

[英]C++: Does vector of pointer elements automatically deallocate dynamic memory pointed by each pointer upon destruction?

标题是不言自明的-标准向量实现是否负责释放向量中所有指针所指向的动态内存?

No. When you destroy a std::vector it destroys all its elements (calls their destructor) and then deallocates the storage used by the objects. 不。当您破坏std::vector它将破坏其所有元素(称为析构函数),然后释放对象使用的存储空间。 But a (raw) pointer does not have a destructor - destroying it does not deallocate the object it points to - it just destroys the storage used to hold the pointer itself. 但是,(生)的指针没有析构函数-摧毁它释放它指向的对象-它只是破坏用来存放指针本身的存储空间。

If you had had a vector of smart pointers ( std::unique_ptr or std::shared_ptr ) then it would be a different matter. 如果您有一个智能指针vectorstd::unique_ptrstd::shared_ptr ),那将是另一回事。 Those classes do have destructors and do deallocate what they point to upon destruction ( unique_ptr always, shared_ptr if it's the last object pointing to the contained object, otherwise it just decrements its reference count). 这些类确实具有析构函数,并且确实在销毁时将其指向的对象解除分配(始终为unique_ptr ,如果它是最后一个指向所包含对象的对象,则为shared_ptr ,否则只会减少其引用计数)。

Note: a std::unique_ptr is a very thin wrapper around a raw pointer, that is designed to optimize away completely. 注意: std::unique_ptr是围绕原始指针的非常薄的包装器,旨在完全优化掉指针。 So, using it should have zero overhead over a raw pointer when optimization is enabled. 因此,启用优化后,使用它的原始指针开销应为零。 So it'll give you the semantics you want with no overhead compared to doing the manual memory management - manually. 因此,与手动进行内存管理相比,它将为您提供所需的语义,并且没有任何开销

No it doesn't. 不,不是。

If you want "self-deleting" pointers use smart pointers ( std::unique_ptr or std::shared_ptr ) or (depending on what the pointers are used for) a container such as std::vector , std::array or std::string . 如果要“自我删除”指针,请使用智能指针( std::unique_ptrstd::shared_ptr )或(取决于指针的用途)诸如std::vectorstd::arraystd::string类的容器std::string

No it doesn't. 不,不是。 Containers are not reponsible of the memory management of raw pointers. 容器不负责原始指针的内存管理。 It would be possible to automatically deallocate your pointer elements if they were smart pointers (RAII : https://fr.wikipedia.org/wiki/Resource_acquisition_is_initialization ) 如果指针元素是智能指针,则可以自动取消分配它们(RAII: https//fr.wikipedia.org/wiki/Resource_acquisition_is_initialization

You can see a pointer as a simple integer. 您可以看到一个简单的整数指针。 Its value represents a memory address. 它的值代表一个内存地址。 When the vector pointer element is deleted, the bytes allocated to store this address are freed. 删除向量指针元素后,将释放分配用于存储该地址的字节。 Thus, the memory address pointed by the pointer is lost (No more reference to it = memory leak). 因此,指针指向的内存地址丢失了(不再引用它=内存泄漏)。

Containers will never manipulate your instances (Free pointers, modify content). 容器永远不会操纵您的实例(释放指针,修改内容)。 It can only call constructors (Specified one, copy, move...) and the destructor. 它只能调用构造函数(指定一个,复制,移动...)和析构函数。

Depends on the what pointers the vector is containing, for raw pointers like 取决于vector所包含的指针,例如原始指针

std::vector<Something*> 

no, you have to do the cleanup yourself. 不,您必须自己进行清理。

If the vector contains smart pointers, on the other hand, like std::unique_ptr 另一方面,如果vector包含智能指针,则类似于std::unique_ptr

std::vector<std::unique_ptr<Something>> 

then the cleanup is taken care of for you. 那么清理工作将为您解决。

Long story short: try to use smart pointers. 长话短说:尝试使用智能指针。

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

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