考虑下面的程序.它已从简单的案例中简化.除非删除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;
}
Run Code Online (Sandbox Code Playgroud)
这是虚拟析构函数存在时我的实现中程序的输出:
分配的内存地址从以下位置开始:0x8895008要取消分配的内存从:0x889500c开始
RUN FAILED(退出值1)
请不要问它应该做什么程序.正如我所说,它来自一个更复杂的案例,其中Arena是各种类型内存的接口.在此示例中,内存仅从堆中分配和释放.
this不是newat行返回的指针char* p = new char[s];你可以看到那里的大小s超过5个Obj实例.差异(应该是sizeof (std::size_t))在附加内存中,包含数组的长度,5,紧接在包含的地址之前this.
好的,规范说清楚了:
http://sourcery.mentor.com/public/cxx-abi/abi.html#array-cookies
2.7阵列运营商新的Cookies
当operator new用于创建新数组时,通常会存储cookie以记住分配的长度(数组元素的数量),以便可以正确地释放它.
特别:
如果数组元素类型T有一个简单的析构函数(12.4 [class.dtor])并且通常的(数组)释放函数(3.7.3.2 [basic.stc.dynamic.deallocation])函数不带两个参数,则不需要cookie .
因此,析构函数的虚拟性是无关紧要的,重要的是析构函数是非平凡的,您可以通过删除virtual析构函数前面的关键字并观察程序崩溃来轻松检查.
| 归档时间: |
|
| 查看次数: |
308 次 |
| 最近记录: |