// Class
ArrayIntVector : IntVector{
private:
int *data;
int dataCapacity;
int numElements;
void check_invariants() const;
}
// Constructor
ArrayIntVector::ArrayIntVector(int initCapacity)
: dataCapacity(initCapacity), numElements(0) {
data = new int[dataCapacity];
check_invariants();
}
// Destructor
ArrayIntVector::~ArrayIntVector() {
check_invariants();
delete[] data;
data = 0;
}
int main(){
IntVector *v = new ArrayIntVector(5);
// testing class functions
// push_back, pop_back, empty, index, grow
delete v;
return 0;
}
我正在泄漏。当我使用valgrind时,我得到以下内容:
HEAP SUMMARY: 在退出时使用:1个块中的20个字节 总堆使用量:7个分配,7个释放,1,284个字节分配
1个块中的20个字节肯定在丢失记录1中丢失1 at 0x4A07152:operator new [](unsigned long)(vg_replace_malloc.c:363) by 0x400DBE:ArrayIntVector :: ArrayIntVector(int)(IntVector.cpp:12) by 0x401142:main(lab09.cpp:8)
答案 0 :(得分:3)
问题是你的析构函数不是虚拟的。将析构函数声明为虚拟。