相关疑难解决方法(0)

正确销毁指向对象的指针

我想问一些关于正确销毁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_;

}
Run Code Online (Sandbox Code Playgroud)

我对矢量指针有同样的问题:

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

When I use it in a method, I would:
this->myVector_ = …
Run Code Online (Sandbox Code Playgroud)

c++ pointers memory-management

9
推荐指数
1
解决办法
6827
查看次数

标签 统计

c++ ×1

memory-management ×1

pointers ×1