涉及类析构函数和删除操作符的几个内存管理问题? [英] A few memory management questions involving class destructors and delete operator?

查看:140
本文介绍了涉及类析构函数和删除操作符的几个内存管理问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读一些教程后,我仍然不清楚关于C ++内存管理的一些要点。



1。
当使用new操作符声明的类超出范围时,它的析构函数被调用和内存释放?
是否有必要调用delete操作符释放类的内存并调用它的析构函数?

  {}; 

void newTest(){
Test * t = new Test;
}

int main()
{
newTest();
return 0;
}

2。
当它们驻留在其中的类时,用new关键字声明的变量(例如向量)是否被销毁?
这些变量是否必须在类的析构函数中显式删除?

  class Test {
vector& int> * vec;
public:
Test();
};

Test :: Test(){
* vec = new vector< int>
}

void newTest(){
Test t;
}

int main()
{
newTest();
return 0;
}

3。
与问题2相同,但在堆栈上声明了正常变量。
在下面的例子中,当t超出范围时,向量vec是否被删除?

  class Test {
vector< int> vec;
};

void newTest(){
Test t;
}

int main()
{
newTest();
return 0;
}

4。
最后,这是更好的方式来声明一个类中的向量,通常(在堆栈上)或使用new关键字(在堆上)?

解决方案

这听起来像你可能来自Java背景,所以 new 在C ++中工作有点不同。


  1. 分配给 new 的内存必须使用删除


  2. 与问题1类似,您必须删除类析构函数中的内存以回收。


  3. 在这种情况下,向量将自动被破坏。


  4. < (相对于新),除非你有特殊的理由,否则。只是因为它在类中存储的值并不意味着它在栈上,它在包含类占据的内存的同一个区域。


最后请注意,在C ++中,您可以使用RAII使内存管理更容易出错。例如,查看shared_ptr,scoped_ptr和unique_ptr。这些都会在适当时自动释放分配的内存。


After reading some tutorials I am still unclear on some points about memory management in C++.

1. when a class declared with the new operator goes out of scope is its destructor called and memory freed? Is it necessary to call the delete operator to free the class' memory and have its destructor called?

class Test{};

void newTest(){
    Test *t = new Test;
}

int main()
{
    newTest();
    return 0;
}

2. Are variables (such as a vector) declared with the new keyword freed when the class they reside within is destroyed? Do those variables have to be explicitly deleted in the class's destructor?

class Test{
    vector<int> *vec;
public:
    Test();
};

Test::Test(){
    *vec = new vector<int>;
}

void newTest(){
    Test t;
}

int main()
{
    newTest();
    return 0;
}

3. Same as question 2, but with normal variables declared on the stack. In the following example, is the vector vec deleted when t goes out of scope?

class Test{
    vector<int> vec;
};

void newTest(){
    Test t;
}

int main()
{
    newTest();
    return 0;
}

4. Finally, which is the better way to declare a vector in a class, normally (on the stack) or with the new keyword (on the heap)?

解决方案

It sounds like you may come from a Java background, so things with new work a bit differently in C++.

  1. The memory allocated with new must be explicitly destroyed with delete. This example has a memory leak.

  2. Similar to question 1, you must delete the memory in the class destructor to reclaim it.

  3. In this case the vector will be destructed automatically.

  4. Within a class, always prefer to store members by value (as opposed to with new) unless you have a specific reason to do otherwise. Just because it's stored by value within the class doesn't mean it's on the stack, it's in the same region of memory that the containing class occupies.

Finally note that in C++ you can use RAII to make memory management much less error prone. Take a look at shared_ptr, scoped_ptr, and unique_ptr for example. These all handle automatically freeing the newly allocated memory when appropriate.

这篇关于涉及类析构函数和删除操作符的几个内存管理问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆