我正在尝试创建一个结构向量,每个结构都有一个指针数组。但是,如果没有内存问题,我似乎无法删除向量。
当我运行 valgrind
==29801== 无效的 free() / delete / delete[] / realloc() ==29801== at 0x4A05A36: operator delete (vg_replace_malloc.c:515) ==29801== by 0x4009D4: test_struct::~test_struct( ) (在 /home/hltcoe/rcotterell/code-switching/a.out) ==29801== by 0x40142B: void std::_Destroy(test_struct*) (在 /home/hltcoe/rcotterell/code-switching/a. out) ==29801== by 0x401299: void std::_Destroy_aux::__destroy(test_struct*, test_struct*) (in /home/hltcoe/rcotterell/code-switching/a.out)
编辑
#include <vector>
using namespace std;
struct test_struct {
public:
int * array;
test_struct() {
array = NULL;
}
~test_struct() {
delete[] array;
}
private:
test_struct(const test_struct& that);
test_struct& operator=(const test_struct& that);
};
int main(int argc, char ** argv) {
vector <test_struct> data;
for (int i = 0; i < 5; ++i) {
test_struct tmp;
tmp.array = new int[5];
data.push_back(tmp);
}
}
它给出了以下编译错误。有任何想法吗?