Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 正确销毁指向对象的指针_C++_Pointers_Memory Management - Fatal编程技术网

C++ 正确销毁指向对象的指针

C++ 正确销毁指向对象的指针,c++,pointers,memory-management,C++,Pointers,Memory Management,我想问几个关于int指针和向量指针的正确销毁的快速问题。首先,我看到人们过去问过这些类型的问题,并且几乎总是有几个关于如何使用C++中的向量指针、指针到对象等不是标准的C++编码实践的回答,并且应该实例化对象的副本。这可能是真的,但你并不总是能够控制在你到来之前已经展示的范例。我需要使用的范例需要初始化指向几乎所有东西的指针。一个非常类似java的C++方法。我们这样做的主要原因之一是我们的数据集太大,堆栈分配可能会溢出 我的问题是: 如果我有一个指向int32_t数组的指针,那么在析构函数中销

我想问几个关于int指针和向量指针的正确销毁的快速问题。首先,我看到人们过去问过这些类型的问题,并且几乎总是有几个关于如何使用C++中的向量指针、指针到对象等不是标准的C++编码实践的回答,并且应该实例化对象的副本。这可能是真的,但你并不总是能够控制在你到来之前已经展示的范例。我需要使用的范例需要初始化指向几乎所有东西的指针。一个非常类似java的C++方法。我们这样做的主要原因之一是我们的数据集太大,堆栈分配可能会溢出

我的问题是:

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

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

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 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?
我将其初始化为成员变量。
std::vector*myVector\uux;
当我在方法中使用它时,我会:
这->myVector_u2;=新的std::vector();
//推回一些物体
要删除方法中的向量,我将迭代向量并删除其对象,然后删除向量;
对于(std::vector::iterator itr=this->myVector\->begin();
its!=this->myVector->end();++itr){
删除(*itr);
}
删除此->myVector;
在析构函数中清理它的正确方法是什么?
我可以删除向量吗?
删除此->myVector;
或者我需要再次迭代整个向量吗?

提前感谢您的建议。

分配给
new
的任何内容都应处理
delete

int* p = new int;
delete p;

使用
new[]
分配的任何内容都应通过
delete[]
解除分配

int* p = new int[10];
delete [] p;
任何动态分配和存储在
向量中的内容都需要手动解除分配:

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);
但是,我认为动态分配
std::vector
的原因很少

在C++11中,最好的方法是使用
std::unique\u 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!
但即使如此,将它们作为
std::unique\u ptr
也更有意义,您甚至可以在需要时将它们视为接口中的原始指针:

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_;
};
class-Foo
{
Foo()
:bar_(新整数)
,bar2_(新整数[20])
{
}
int*get_bar()
{
返回条get();
}
int*get_bar2()
{
返回bar2_u2;.get();
}
std::唯一的ptr条;
std::唯一的\u ptr bar2;
};

new[]
-->
delete[]
new
-->
delete
。为什么要使用
向量*
std::vector
不会在堆栈上放置大量分配(在内部它从堆中分配对象),因此通常没有很好的理由动态分配
std::vector
。@Chad使用向量指针是有充分理由的;一种情况是,如果API已经有工厂向您提供向量,但您有责任销毁它。代码没有“指向[n]
int32\t
数组的指针”。它有一个指向
int32\t
的指针。强制用户进行内存管理的API是邪恶的。关于delete/delete[]的规则并不总是正确的。考虑<代码> TyPulf int MyType [ 42 ];int*x=新的mytype。现在需要
删除[]x即使您使用了
new
而不是
new[]
。谢谢。我真的很感激。因此,在int32_t*上的示例中;在我的方法中,我将其初始化为myArray=new int32\u t[50]。在该方法中,我将使用delete[]myArray\uu删除它,但在析构函数中,我将使用delete myArray\uu,因为它不再被定义为new int32\u50]?不,如果使用
new[]
在初始化它,则使用
delete[]
,即使它在另一个(稍后的)方法中也是如此。我已经更新了我的答案,补充了一些细节。@Chad:因为您在顶部仍然有错误的规则“任何分配给new的内容都应通过delete处理”。@etarion您能解释一下为什么这是一个问题吗?智能指针(例如
std::unique_ptr
)是否将该规则抽象掉,在用户代码中可能会看到
新的
,而在用户代码中没有相应的
删除
class Foo
{
   Foo()
      : bar_(new int)
      , bar2_(new int[20])
   {
   }

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

   int* bar_;
   int* bar2_;
};
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_;
};