我用指针来创建一个数组,然后在析构函数Valgrind的抱怨内存泄漏,但我打电话new和delete
class cBuffer{
private:
struct entry {
uint64_t key;
uint64_t pc;
};
entry *en;
public:
cBuffer(int a, int b, int mode)
{
limit = a;
dist = b;
md = mode;
en = new entry[ limit ];
for (int i=0; i<limit; i++) {
en[i].key = 0;
en[i].pc = 0;
}
};
~cBuffer() { delete [] en; }
...
}
在另一大类我用cBuffer这样写道删除过程:
class foo() {
cBuffer *buf;
foo()
{
buf = new cBuffer(gSize, oDist, Mode);
}
};
然而,Valgrind的抱怨新的运营商
==20381== 16,906,240 bytes in 32 blocks are possibly lost in loss record 11,217 of 11,221
==20381== at 0x4A0674C: operator new[](unsigned long) (vg_replace_malloc.c:305)
==20381== by 0x166D92F8: cBuffer::cBuffer(int, int, int)
您没有遵循三个规则。 – 2013-04-28 14:37:26
见:https://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming) – 2013-04-28 14:40:43
@mahmood:见[这里](http://stackoverflow.com/questions/4172722);如果你管理资源,并在析构函数释放它,那么你几乎肯定需要落实或删除拷贝构造函数和拷贝赋值运算符,否则,你可以用两个对象最终都试图释放相同的资源。 – 2013-04-28 14:41:17