删除动态分配的锯齿状数组
我有一个指向整数数组的指针数组。 我已经制作了一个硬编码的整数数组(检查下面的“数组”),我想将其插入到指针数组中(检查下面的“栏”),据我所知,它仍然是一个指针数组指向整数数组。
它编译得很好;没有警告。 运行良好;并关闭良好; 至少,直到我在析构函数中引入当前注释掉的删除语句。
我想我的编译器此时会创建一个更好的析构函数,但我对我做错了什么感兴趣。终端窗口只是抛出一个巨大的内存映射;谷歌搜索和搜索并没有多大帮助。
class foo {
public:
int **bar;
int aSize;
//CONSTRUCTOR
foo(int aSize) {
this->aSize = aSize;
bar = new int*[aSize];
for (int i=0;i<aSize;i++) {
bar[i] = new int[aSize + i];
for (int j=0;j<(aSize + i);j++) {
bar[i][j] = 0;
}
}
}
void myfunc(int *pointer) {
bar[0] = pointer;
}
~foo() {
for (int i=0;i<aSize;i++) {
//delete[] bar[i];
}
delete[] bar;
}
};
int main() {
foo *obj = new foo(5);
int array[] = {1,2,3,4};
obj->myfunc(array);
delete obj;
return 0;
};
我知道我可能做了一些悲惨的事情;我只是还不知道它是什么。 我不使用STL或其他模板是有原因的,只是因为我有兴趣学习这个。 最大程度的批评表示赞赏。
I have an array of pointers that point to arrays of ints.
I have made a hard coded array of ints (check 'array' below) and I want to insert it into the array of pointers (check 'bar' below), as far as I can tell, it is still an array of pointers that point to arrays of ints.
It compiles fine; no warnings.
It runs fine; and closes fine;
Until at least, I introduce the currently commented out delete statement in the destructor.
I imagine my compiler would make a better destructor at this point, but I'm interested as to what I'm doing wrong. The terminal window just throws out a gigantic memory map; and googling and searching SO didn't help much.
class foo {
public:
int **bar;
int aSize;
//CONSTRUCTOR
foo(int aSize) {
this->aSize = aSize;
bar = new int*[aSize];
for (int i=0;i<aSize;i++) {
bar[i] = new int[aSize + i];
for (int j=0;j<(aSize + i);j++) {
bar[i][j] = 0;
}
}
}
void myfunc(int *pointer) {
bar[0] = pointer;
}
~foo() {
for (int i=0;i<aSize;i++) {
//delete[] bar[i];
}
delete[] bar;
}
};
int main() {
foo *obj = new foo(5);
int array[] = {1,2,3,4};
obj->myfunc(array);
delete obj;
return 0;
};
I know I've probably done something tragic; I just don't know what it is yet.
There is a reason why I am not using STL or other templates, it is simply because I'm interested in learning this.
Maximum criticism appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
myfunc
函数接收一个指针,然后将bar[0]
设置为该内存地址。在示例代码中,您将数组的地址传递给它,这是一个自动(堆栈)变量。然后,析构函数尝试删除[] bar[0]
,它指向堆栈变量。这完全是未定义行为,也是程序崩溃的原因。您无法删除[]
堆栈数组。您只能delete[]
使用new[]
分配的数组。此外,
myfunc
函数总是会泄漏内存,因为bar[0]
指向堆分配的数组。当您将bar
设置为不同的内存地址而没有先delete[]
之前的地址时,就会泄漏内存。The
myfunc
function takes in a pointer, and then setsbar[0]
to that memory address. In your example code, you pass it the address ofarray
, which is an automatic (stack) variable. The destructor then attempts todelete[] bar[0]
, which points to a stack variable. This is completely undefined behavior, and the reason your program is crashing. You can'tdelete[]
a stack array. You can onlydelete[]
an array which was allocated usingnew[]
.Also, the
myfunc
function is always going to leak memory, becausebar[0]
points to a heap-allocated array. When you setbar
to a different memory address without firstdelete[]
ing the previous address, you are leaking memory.问题出在调用
myfunc
。在该函数中,您将替换您的类认为它拥有的指针 (bar[0]
)。然后,您的obj
析构函数将尝试在main
中的array[]
上运行delete[]
,这可能是导致您的崩溃并使您原来的bar[0]
悬空。The problem is calling
myfunc
. In that function, you are replacing a pointer (bar[0]
) that your class thinks it owns. Yourobj
destructor will then consequently try to rundelete[]
on yourarray[]
inmain
, which probably causes your crash and leaves your originalbar[0]
dangling.