考虑下面的程序。它已从一个复杂的案例简化而来。除非我删除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;
}
这是我的实现中当虚拟析构函数存在时程序的输出:
请不要问这个程序应该做什么。正如我所说,它来自一个更复杂的案例,其中Arena是各种类型内存的接口。在这个示例中,内存只是从堆中分配和释放的。分配的内存地址开始于:0x8895008 要释放的内存开始于:0x889500c
运行失败(退出值为1)