如何清理(析构)动态指针数组?
析构函数就足够了还是我必须迭代才能删除新节点?
#include "stdafx.h"
#include<iostream>
using namespace std;
struct node{
int row;
int col;
int value;
node* next_in_row;
node* next_in_col;
};
class MultiLinkedListSparseArray {
private:
char *logfile;
node** rowPtr;
node** colPtr; // used in constructor
node* find_node(node* out);
node* ins_node(node* ins,int col);
node* in_node(node* ins,node* z);
node* get(node* in,int row,int col);
bool exist(node* so,int row,int col);
//add anything you need
public:
MultiLinkedListSparseArray(int rows, int cols);
~MultiLinkedListSparseArray();
void setCell(int row, int col, int value);
int getCell(int row, int col);
void display();
void log(char *s);
void dump();
};
MultiLinkedListSparseArray::MultiLinkedListSparseArray(int rows,int cols){
rowPtr=new node* [rows+1];
colPtr=new node* [cols+1];
for(int n=0;n<=rows;n++)
rowPtr[n]=NULL;
for(int i=0;i<=cols;i++)
colPtr[i]=NULL;
}
MultiLinkedListSparseArray::~MultiLinkedListSparseArray(){ // is that destructor enough??
cout<<"array is deleted"<<endl;
delete [] rowPtr;
delete [] colPtr;
}
Is that Destructor is enough or do I have to iterate to delete the new nodes??
#include "stdafx.h"
#include<iostream>
using namespace std;
struct node{
int row;
int col;
int value;
node* next_in_row;
node* next_in_col;
};
class MultiLinkedListSparseArray {
private:
char *logfile;
node** rowPtr;
node** colPtr; // used in constructor
node* find_node(node* out);
node* ins_node(node* ins,int col);
node* in_node(node* ins,node* z);
node* get(node* in,int row,int col);
bool exist(node* so,int row,int col);
//add anything you need
public:
MultiLinkedListSparseArray(int rows, int cols);
~MultiLinkedListSparseArray();
void setCell(int row, int col, int value);
int getCell(int row, int col);
void display();
void log(char *s);
void dump();
};
MultiLinkedListSparseArray::MultiLinkedListSparseArray(int rows,int cols){
rowPtr=new node* [rows+1];
colPtr=new node* [cols+1];
for(int n=0;n<=rows;n++)
rowPtr[n]=NULL;
for(int i=0;i<=cols;i++)
colPtr[i]=NULL;
}
MultiLinkedListSparseArray::~MultiLinkedListSparseArray(){ // is that destructor enough??
cout<<"array is deleted"<<endl;
delete [] rowPtr;
delete [] colPtr;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
黄金法则是每次调用 new 时都必须调用 delete 一次。如果只调用一次 new 来构造数组,则只需要调用一次 delete 来销毁它。
如果您在将某个项目添加到列表之前调用 new,并且忘记了该项目,则您还必须在析构函数中对这些项目调用 delete。
上面您正在初始化一个指针数组,并且每个指针仅 4 个字节会被正确释放。
我通常跟踪代码中每个项目的责任,如果将一个项目添加到列表中,列表是否会接管该对象的责任?如果你有类似 new 的项目,将其添加到列表中并销毁指向新对象的指针,只留下列表及其引用,那么列表必须承担清理内存的责任,并且它必须迭代这些项目并将它们一一释放。
The golden rule is that you have to call delete once for every time you called new. If you only call new once to construct the array you only need to call delete once to destroy it.
If you call new on an item before adding it to the list and forget about the item you have to call delete on those as well in the destructor.
Above you are initializing an array of pointers, and the 4 bytes per pointer only will be freed correctly.
I usually track the responsibility of each item in my code, if you add an item to the list, does the list take over responsibility for the object? If you have something like new the item, add it to the list and destroy the pointer to the new object, leaving only the list with a reference to it, then the list has to take over responsibility for cleaning the memory and it would have to iterate the items and free them one by one.
使用现成的容器来存储处理指针的数组。很少需要自己推出,如果有可能,则将其概括化,以便可以重复使用。
Use an off-the-shelf container for arrays that handle pointers. There is rarely a need to roll your own, and if perchance there is then generalize it so that it can be reused.
如果您在这些数组上插入了对象指针(最初将它们初始化为 NULL),则需要迭代它们以删除每个单个对象。
一如既往,每新增一个删除。
If you inserted object pointers on those arrays(initially you are initializing them to NULL), you need to iterate them to delete each single object.
As always, one delete for each new.