考虑下面的程序。它已经从一个复杂的案例中简化了。除非我删除 Obj 类中的虚拟析构函数,否则它无法删除先前分配的内存。我不明白为什么程序输出的两个地址不同,只有在虚拟析构函数存在的情况下。
// GCC 4.4
#include <iostream>
using namespace std;
class Arena {
public:
void* alloc(size_t s) {
char* p = new char[s];
cout << "Allocated memory address starts at: " << (void*)p << '\n';
return p;
}
void free(void* p) {
cout << "The memory to be deallocated starts at: " << p << '\n';
delete [] static_cast<char*> (p); // the program fails here
}
};
struct Obj {
void* operator new[](size_t s, Arena& a) {
return a.alloc(s);
}
virtual ~Obj() {} // if I remove this everything works as expected
void destroy(size_t n, Arena* a) {
for (size_t i = 0; i < n; i++)
this[n - i - 1].~Obj();
if (a)
a->free(this);
}
};
int main(int argc, char** argv) {
Arena a;
Obj* p = new(a) Obj[5]();
p->destroy(5, &a);
return 0;
}
当虚拟析构函数存在时,这是我的实现中程序的输出:
分配的内存地址开始于:0x8895008 要释放的内存开始于:0x889500c
运行失败(退出值 1)
请不要问它应该做什么程序。正如我所说,它来自一个更复杂的案例,其中 Arena 是各种类型内存的接口。在这个例子中,内存只是从堆中分配和释放。