带有向量、指针的 C++ 析构函数,
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12068950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Destructors with Vectors, Pointers,
提问by ISTB
As far as I know, I should destroy in destructors everything I created with new
and close opened filestreams and other streams.
However, I have some doubts about other objects in C++:
据我所知,我应该在析构函数中销毁我创建的所有内容new
并关闭打开的文件流和其他流。但是,我对 C++ 中的其他对象有一些疑问:
std::vector
andstd::string
s: Are they destroyed automatically?If I have something like
std::vector<myClass*>
of pointers to classes. What happens when the vector destructor is called?
Would it call automatically the destructor ofmyClass
? Or only the vector is destroyed but all the Objects it contains are still existant in the memory?What happens if I have a pointer to another class inside a class, say:
class A { ClassB* B; }
and Class A is destroyed at some point in the code. Will Class B be destroyed too or just the pointer and class B will be still existent somewhere in the memory?
std::vector
和std::string
s:它们会自动销毁吗?如果我有类似的东西
std::vector<myClass*>
类的指针。调用向量析构函数时会发生什么?
它会自动调用 的析构函数myClass
吗?或者只有向量被销毁,但它包含的所有对象仍然存在于内存中?如果我在一个类中有一个指向另一个类的指针会发生什么,比如:
class A { ClassB* B; }
并且 A 类在代码中的某个点被销毁。B 类是否也会被销毁,或者只是指针和 B 类仍然存在于内存中的某处?
回答by hmjd
std::vector and std::strings: Are they destroyed automatically?
std::vector 和 std::strings:它们会自动销毁吗?
Yes (assuming member variables are not pointers to std::vector
and std::string
).
是(假设成员变量不是指向std::vector
and 的指针std::string
)。
If I have something like std::vector what happens when the vector destructor is called? Would it call automatically the destructor of myClass? Or only the vector is destroyed but all the Objects it contains are still existant in the memory?
如果我有类似 std::vector 的东西,当调用向量析构函数时会发生什么?它会自动调用 myClass 的析构函数吗?或者只有向量被销毁,但它包含的所有对象仍然存在于内存中?
If vector<MyClass>
then all objects contained in the vector will be destroyed. If vector<MyClass*>
then all objects must be explicitly delete
d (assuming the class being destructed owns the objects in the vector
). A third alternative is vector
of smart pointers, like vector<shared_ptr<MyClass>>
, in which case the elements of the vector
do not need to be explictly delete
d.
如果vector<MyClass>
那么包含在向量中的所有对象都将被销毁。如果vector<MyClass*>
那么所有对象都必须显式delete
d (假设被销毁的类拥有 中的对象vector
)。第三种选择是vector
智能指针,例如vector<shared_ptr<MyClass>>
,在这种情况下 的元素vector
不需要显式delete
d。
What happens if I have a pointer to another class inside a class
如果我在一个类中有一个指向另一个类的指针会发生什么
The B
must be explicitly delete
d. Again, a smart pointer could be used to handle the destruction of B
.
在B
必须明确delete
天。同样,可以使用智能指针来处理B
.
回答by ElBaulP
You only need to worry about for the memory you have created dynamically (When you reserve memory with new
.)
您只需要担心动态创建的内存(当您使用new
.保留内存时)
For example:
例如:
Class Myclass{
private:
char* ptr;
public:
~Myclass() {delete[] ptr;};
}
回答by Cubic
It depends. std::vector
and std::string
and MyClass
all have 1 thing in common - if you declare a variable to be any of those types, then it will be allocated on stack, be local to the current block you're in, and be destructed when that block ends.
这取决于。std::vector
并std::string
与MyClass
所有的共同点一样东西-如果你声明一个变量是任何这些类型的,那么它会在栈上分配,是本地的当前块你能加入,和该块结束时被破坏。
E.g.
例如
{
std::vector<std::string> a;
std::string b;
MyClass c;
} //at this point, first c will be destroyed, then b, then all strings in a, then a.
If you get to pointers, you guessed correctly: Only the memory the pointer itself occupies (usually a 4 byte integer) will be automatically freed upon leaving scope. Nothing happens to the memory pointed to unless you explicitly delete
it (whether it's in a vector or not). If you have a class that contains pointers to other objects you mayhave to delete them in the destructor (depending on whether or not that class ownsthose objects). Note that in C++11 there are pointer classes (called smart pointers) that let you treat pointers in a similar fashion to 'normal' objects:
如果你使用指针,你猜对了:只有指针本身占用的内存(通常是一个 4 字节的整数)会在离开作用域时自动释放。除非您明确指出,否则指向的内存不会发生任何事情delete
(无论它是否在向量中)。如果您有一个包含指向其他对象的指针的类,您可能必须在析构函数中删除它们(取决于该类是否拥有这些对象)。请注意,在 C++11 中有指针类(称为智能指针),可以让您以类似于“普通”对象的方式处理指针:
Ex:
前任:
{
std::unique_ptr<std::string> a = make_unique<std::string>("Hello World");
function_that_wants_string_ptr(a.get());
} //here a will call delete on it's internal string ptr and then be destroyed
回答by Luchian Grigore
if they are in automatic storage, yes. You can have
std::string* s = new std::string
, in which case you have to delete it yourself.nothing, you need to manually delete memory you own (for memory allocated with
new
).if you allocated
b
withnew
, you should destroy it in the destructor explicitly.
如果它们在自动存储中,是的。你可以有
std::string* s = new std::string
,在这种情况下你必须自己删除它。没什么,您需要手动删除您拥有的内存(对于使用 分配的内存
new
)。如果您分配
b
使用new
,你应该在析构函数销毁它明确。
A good rule of thumb is to use a delete/delete[]
for each new/new[]
you have in your code.
一个好的经验法则是对代码中的delete/delete[]
每个使用new/new[]
。
A better rule of thumb is to use RAII, and use smart pointers instead of raw pointers.
更好的经验法则是使用 RAII,并使用智能指针而不是原始指针。
回答by Andrey
If I have something like std::vector what happens when the vector destructor is called?
如果我有类似 std::vector 的东西,当调用向量析构函数时会发生什么?
It depends.
这取决于。
If you have a vector of valuesstd::vector <MyClass>
, then the destructor of the vector calls the destructor for every instance of MyClass
in the vector.
如果您有一个值向量std::vector <MyClass>
,那么向量的析构函数会为向量中的每个实例调用析构函数MyClass
。
If you have a vector of pointersstd::vector <MyClass*>
, then you're responsible for deleting the instances of MyClass
.
如果您有一个指针向量std::vector <MyClass*>
,那么您有责任删除 的实例MyClass
。
What happens if I have a pointer to another class inside a class
如果我在一个类中有一个指向另一个类的指针会发生什么
ClassB
instance would remain in memory. Possible ways to have ClassA
destructor to make the job for you are to make B
an instance member or a smart pointer.
ClassB
实例将保留在内存中。让ClassA
析构函数为您完成工作的可能方法是创建B
实例成员或智能指针。
回答by Andrey
std::vector
, std::string
and as far as I know all other STL containers have automatic destructors. This is the reason why it is often better to use these containers instead of new
and delete
since you will prevent memory leaks.
std::vector
,std::string
据我所知,所有其他 STL 容器都有自动析构函数。这就是为什么使用这些容器通常会更好的原因new
,delete
因为您将防止内存泄漏。
Your myClass
destructor will only be called if your vector is a vector of myClass
objects (std::vector< myClass >
) instead of a vector of pointers to myClass
objects (std::vector< myClass* >
).
你myClass
如果您的载体是一个矢量析构函数才会被调用myClass
的对象(std::vector< myClass >
)而不是指针到矢量myClass
对象(std::vector< myClass* >
)。
In the first case the destructor of std::vector
will also call the destructor of myClass
for each of its elements; in the second case the destructor of std::vector
will call the destructor of myClass*
, which means it will free the space taken to store each pointer but will not free the space taken to store the myClass
objects themselves.
在第一种情况下, 的析构函数也std::vector
将myClass
为其每个元素调用析构函数;在第二种情况下, 的析构函数std::vector
将调用 的析构函数myClass*
,这意味着它将释放用于存储每个指针的空间,但不会释放用于存储myClass
对象本身的空间。
The Class B
objects you point to will not be destroyed, but the space assigned to store its pointer will be freed.
Class B
您指向的对象不会被销毁,但分配给存储其指针的空间将被释放。
回答by Andrey
Yes.
std::vector
andstd::string
are automatically when they finish out of scope, calling also the destructor of the objects contained (forstd::vector
).As said before,
std::vector
is destroyed when it finish out of scope, calling the destructor of the objects contained. But in fact, in this case, the objects contained are the pointers, not the object pointed by the pointers. So you have todelete
them manually.The same as (2). A will be destroyed and so the pointer, but not the class B pointed. You have to provide a destructor for A that
delete
B.
是的。
std::vector
并且std::string
在它们超出范围时自动调用,同时调用所包含对象的析构函数(forstd::vector
)。如前所述,
std::vector
当它超出范围时被销毁,调用所包含对象的析构函数。但实际上,在这种情况下,包含的对象是指针,而不是指针指向的对象。所以你必须delete
手动处理它们。同(2)。A 将被销毁,因此指针将被销毁,但 B 类不会被销毁。你必须为
delete
B提供一个析构函数。
In C++11 there is a very useful solution: use std::unique_pointer
. Can be use only to point a single object and this will be deleted when the pointer goes out of scope (for example when you destroy your std::vector
).
在 C++11 中有一个非常有用的解决方案:使用std::unique_pointer
. 只能用于指向单个对象,当指针超出范围时(例如,当您销毁您的std::vector
)时,它将被删除。