在 C++ 中,如何编写析构函数来释放指向结构的指针的内存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14993791/
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
In C++, how to write a destructor for freeing memory of pointer to a structure?
提问by user13107
Here's my structure A
这是我的结构 A
struct A {
int a1;
int a2;
~A() { }
};
B
is another structure that contains a pointer to A
B
是另一个包含指向 A 的指针的结构
struct B {
B(int b, A* a)
: b1(b), ptr2A(a)
{}
int b1;
A* ptr2A;
~B() {
delete b1;
// traverse each element pointed to by A, delete them <----
}
};
Later on I use below code
后来我使用下面的代码
int bb1;
vector <A*> aa1;
// do some stuff
B *ptrB = new B(bb1, aa1);
I need to delete/free all the memory pointed to by ptrB. Hence I need to write correct destructor inside struct B. How do I traverse each element pointed to by A and delete them?
我需要删除/释放 ptrB 指向的所有内存。因此,我需要在 struct B 中编写正确的析构函数。如何遍历 A 指向的每个元素并删除它们?
回答by Carl
If you're using a C++11 compiler, just use std::shared_ptr and you don't have to worry about deletes. This is because the shared_ptr is a "smart" pointer that will automatically delete what its pointing to.
如果您使用的是 C++11 编译器,只需使用 std::shared_ptr 就不必担心删除。这是因为 shared_ptr 是一个“智能”指针,它会自动删除它指向的内容。
#include <memory>
struct B
{
int b1;
std::shared_ptr<A> ptr2A;
B(int b, std::shared_ptr<A> a):b1(b),ptr2A(a)({}
~B(){} //look ma! no deletes!
};
Use a shared pointer whenever you allocate something:
每当你分配一些东西时使用共享指针:
#include<memory>
...
{
....
std::shared_ptr<B> ptrB( new B(bb1, aa1) );
//Here is another, more readable way of doing the same thing:
//auto ptrB = std::make_shared<B>(bb1,aa1);
...
}
//no memory leaks here, because B is automatically destroyed
Here's more infoon the subject of smart pointers.
I should also mention that if you don't have a C++11 compiler, you can get shared pointers from the BOOST library.
我还应该提到,如果您没有 C++11 编译器,您可以从BOOST 库中获取共享指针。
回答by 0x499602D2
You need only to delete
objects allocated by new
. In this case there's no need to delete b1
as it has not been dynamically-allocated. Moreover, if you did not initialize ptr2a
with dynamic memory, deleting it is undefined behavior.
您只需要delete
分配给new
. 在这种情况下,不需要删除,b1
因为它没有被动态分配。此外,如果您没有ptr2a
使用动态内存进行初始化,则删除它是未定义的行为。
So there's no need to worry about deleting A
s data as it will be destructed from memory along wih the instance of the class.
因此无需担心删除A
s 数据,因为它会与类的实例一起从内存中销毁。
回答by Cameron
You've only got one pointer to A
. So you only need to delete that:
你只有一个指向A
. 所以你只需要删除:
~B() {
delete ptr2A;
}
Note that you can't delete
b1, since it's a plain int
! (The memory taken up by the variables of the structure, such as b1
and the pointer ptr2A
itself (not what it points to) are destroyed automatically along with any instances of that structure.)
请注意,您不能delete
b1,因为它是一个普通的int
! (结构的变量占用的内存,例如b1
指针ptr2A
本身(而不是它指向的内容)与该结构的任何实例一起自动销毁。)