在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:52:30  来源:igfitidea点击:

In C++, how to write a destructor for freeing memory of pointer to a structure?

c++struct

提问by user13107

Here's my structure A

这是我的结构 A

struct A {
    int a1;
    int a2;
    ~A() { }
};

Bis 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 deleteobjects allocated by new. In this case there's no need to delete b1as it has not been dynamically-allocated. Moreover, if you did not initialize ptr2awith dynamic memory, deleting it is undefined behavior.

您只需要delete分配给new. 在这种情况下,不需要删除,b1因为它没有被动态分配。此外,如果您没有ptr2a使用动态内存进行初始化,则删除它是未定义的行为。

So there's no need to worry about deleting As data as it will be destructed from memory along wih the instance of the class.

因此无需担心删除As 数据,因为它会与类的实例一起从内存中销毁。

回答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 deleteb1, since it's a plain int! (The memory taken up by the variables of the structure, such as b1and the pointer ptr2Aitself (not what it points to) are destroyed automatically along with any instances of that structure.)

请注意,您不能deleteb1,因为它是一个普通的int! (结构的变量占用的内存,例如b1指针ptr2A本身(而不是它指向的内容)与该结构的任何实例一起自动销毁。)