gpt4 book ai didi

c++ - 使用析构函数时双重释放或损坏

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:34:46 26 4
gpt4 key购买 nike

<分区>

在下面的代码中,当我添加用箭头指定的行时会出现错误:

Error in `./a.out': double free or corruption (fasttop): 0x00000000007a7030 * Aborted (core dumped)

如果我不使用析构函数,代码就可以工作。有什么想法吗?

#include<iostream>
#include<vector>

struct Element
{
int *vtx;

~Element ()
{
delete [] vtx;
}
};

int main ()
{
Element *elm = new Element [2];
elm[0].vtx = new int [2]; // <----- adding this gives error

std::vector <Element> vec;
vec.push_back (elm[0]);
vec.push_back (elm[0]);

return 0;
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com