删除动态分配的锯齿状数组

发布于 2024-10-11 17:34:56 字数 1012 浏览 4 评论 0原文

我有一个指向整数数组的指针数组。 我已经制作了一个硬编码的整数数组(检查下面的“数组”),我想将其插入到指针数组中(检查下面的“栏”),据我所知,它仍然是一个指针数组指向整数数组。

它编译得很好;没有警告。 运行良好;并关闭良好; 至少,直到我在析构函数中引入当前注释掉的删除语句。

我想我的编译器此时会创建一个更好的析构函数,但我对我做错了什么感兴趣。终端窗口只是抛出一个巨大的内存映射;谷歌搜索和搜索并没有多大帮助。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

演出会有结束 2024-10-18 17:34:57

myfunc 函数接收一个指针,然后将 bar[0] 设置为该内存地址。在示例代码中,您将数组的地址传递给它,这是一个自动(堆栈)变量。然后,析构函数尝试删除[] bar[0],它指向堆栈变量。这完全是未定义行为,也是程序崩溃的原因。您无法删除[]堆栈数组。您只能delete[]使用new[]分配的数组。

此外,myfunc 函数总是会泄漏内存,因为 bar[0] 指向堆分配的数组。当您将 bar 设置为不同的内存地址而没有先delete[]之前的地址时,就会泄漏内存。

The myfunc function takes in a pointer, and then sets bar[0] to that memory address. In your example code, you pass it the address of array, which is an automatic (stack) variable. The destructor then attempts to delete[] bar[0], which points to a stack variable. This is completely undefined behavior, and the reason your program is crashing. You can't delete[] a stack array. You can only delete[] an array which was allocated using new[].

Also, the myfunc function is always going to leak memory, because bar[0] points to a heap-allocated array. When you set bar to a different memory address without first delete[]ing the previous address, you are leaking memory.

浅紫色的梦幻 2024-10-18 17:34:57

问题出在调用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. Your obj destructor will then consequently try to run delete[] on your array[] in main, which probably causes your crash and leaves your original bar[0] dangling.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文