我想創建一個向量,每個結構都有一個指針數組。但是,我似乎無法刪除沒有內存問題的向量。結構與指針的C++向量
當運行的valgrind
== 29801 ==無效免費()/刪除/刪除[]/realloc()的 == 29801 ==在0x4A05A36:操作者刪除(vg_replace_malloc.c:515 ) == 29801 == by 0x4009D4:test_struct ::〜test_struct()(in /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);
}
}
而且它提供了以下編譯錯誤。有任何想法嗎?
可能重複[三條法則是什麼?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-ree) – juanchopanza