作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
<分区>
在下面的代码中,当我添加用箭头指定的行时会出现错误:
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;
}
我是一名优秀的程序员,十分优秀!