正确销毁指向对象的指针 [英] Proper destruction of pointers to objects

查看:103
本文介绍了正确销毁指向对象的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问几个关于正确破坏int指针和vector指针的简短问题.首先,我看到人们过去曾经问过这些类型的问题,并且几乎总是有一些关于在C ++中使用向量指针,指向对象的指针等不是很好的标准c ++编码实践的回答,您应该实例化一个对象的副本.可能是正确的,但您并不总是可以控制到达之前已建立的范例.我需要从事的范例需要初始化指向几乎所有内容的指针.一种非常类似于Java的C ++方法.这样做的主要原因之一是我们的数据集太大,堆栈分配可能会溢出.

I would like to ask a couple of quick questions about the proper destruction of int pointers and vector pointers. First, I have seen people ask in the past about these type of problems, and almost always there are several responses about how using vector pointers , pointers to objects ,etc in C++ is not good standard c++ coding practice, and that you should instantiate a copy of the object. That may be true, but you do not always have control of paradigms that have been layed before your arrival. The paradigm I am required to work in requires initializing pointers to almost everything. A very Java like approach to C++. One of the main reasons we do this is our data sets are so large , stack allocations would be subject to overflows.

我的问题:

如果我有一个指向int32_t数组的指针,那么在析构函数中销毁它的正确方法是什么?

If I have a pointer to a int32_t array, what is the proper way to destroy it in the destructor?

注意:我们的做法是在构造函数中将任何指针设置为NULL.

Note: Our practice is to set any pointer to NULL in the constructor.

I initialize it as a member variable. 
int32_t *myArray_;

When I use it in a method, I would:
this->myArray = new int32_t[50];

To delete it in the method I would call delete on the array:
delete [] this->myArray;

What is the proper call in the destructor?
~MyDestructor(){

    delete this->myArray_;
    or delete [] this->myArray_;

}

我对向量指针也有同样的疑问:

I have the same question about vector pointers:

I initialize it as a member variable. 
std::vector<MyObject*> *myVector_;

When I use it in a method, I would:
this->myVector_ = new std::vector<MyObject*>();


//pushback some objects

To delete the vector in the method I would iterate the vector and delete its objects, then    delete the vector;

 for(std::vector<MyObject*>::iterator itr = this->myVector_->begin(); 
 its != this->myVector->end(); ++ itr){

      delete (*itr);

}

delete this->myVector_;


What would be the proper way to clean it up in the destructor?
would I just delete the vector?

delete this->myVector;

or do I need to iterate the entire vector again?

预先感谢您的任何建议.

Thanks in advance for any advice.

推荐答案

分配给new的所有内容都应分配给delete.

Anything allocated with new should be deallocated with delete.

int* p = new int;
delete p;

分配给new []的所有内容都应分配给delete [].

Anything allocated with new [] should be deallocated with delete [].

int* p = new int[10];
delete [] p;

任何动态分配并存储在vector中的东西,都需要手动释放:

Anything dynamically allocated and stored in a vector, needs to be manually deallocated:

std::vector<int*> v;
v.push_back(new int(1));
v.push_back(new int(2));

for(std::vector<int*>::iterator i = v.begin(), e = v.end(); i != e; ++i)
   delete (*i);

如果出于某种奇怪的原因,您认为动态分配vector是适当的,则应用相同的规则.

If, for some strange reason, you feel it is appropriate to dynamically allocate a vector, the same rules apply.

std::vector<int*>* v = new std::vector<int*>;
v->push_back(new int(1));
v->push_back(new int(2));

for(std::vector<int*>::iterator i = v->begin(), e = v->end(); i != v; ++i)
   delete (*i);

delete v;

但是,我建议动态分配std::vector的原因很少,而且相去甚远.

But, I would suggest that the reasons to dynamically allocate a std::vector are few and far between.

在C ++ 11中,最好的方法是使用std::unique_ptr:

In C++11, the best way to do this, is with std::unique_ptr:

std::unique_ptr<int> p(new int);
// std::unique_ptr handles clean up for you


std::unique_ptr<int[]> p(new int[10]);
// std::unique_ptr handles clean up for you for arrays too!

如果您具有类的成员变量,则适用相同的规则:

If you have a member variable of a class, the same rules apply:

class Foo
{
   Foo()
      : bar_(new int)
      , bar2_(new int[20])
   {
   }

   ~Foo()
   {
      delete [] bar2_;
      delete bar_;
   }

   int* bar_;
   int* bar2_;
};

但是即使那样,将它们设置为std::unique_ptr还是更有意义的,您甚至可以在需要时将它们视为接口中的原始指针:

But even then, it makes more sense to have them as std::unique_ptr, you can even treat them as raw pointers in interfaces when required:

class Foo
{
   Foo()
     : bar_(new int)
     , bar2_(new int[20])
   {
   }

   int* get_bar()
   {
      return bar_.get();
   }

   int* get_bar2()
   {
      return bar2_.get();
   }

   std::unique_ptr<int> bar_;
   std::unique_ptr<int[]> bar2_;
};

这篇关于正确销毁指向对象的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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