使用放置 new[] 有什么问题吗?做
考虑下面的程序。它是由一个复杂的案例简化而来的。除非我删除 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 是各种类型内存的接口。在此示例中,内存只是从堆中分配和释放。
Consider the program below. It has been simplified from a complex case. It fails on deleting the previous allocated memory, unless I remove the virtual destructor in the Obj class. I don't understand why the two addresses from the output of the program differ, only if the virtual destructor is present.
// 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;
}
This is the output of the program in my implementation when the virtual destructor is present:
Allocated memory address starts at: 0x8895008
The memory to be deallocated starts at: 0x889500cRUN FAILED (exit value 1)
Please don't ask what the program it's supposed to do. As I said it comes from a more complex case where Arena is an interface for various types of memory. In this example the memory is just allocated and deallocated from the heap.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
this
不是char* p = new char[s];
行的new
返回的指针,您可以看到大小s
超过 5 个Obj
实例。差异(应该是sizeof (std::size_t)
)在于额外的内存,包含数组的长度 5,紧接在this
中包含的地址之前。好的,规范说得很清楚:
http://sourcery. Mentor.com/public/cxx-abi/abi.html#array-cookies
因此,析构函数的虚拟性质是无关紧要的,重要的是析构函数是不平凡的,您可以通过删除前面的关键字
virtual
来轻松检查这一点析构函数并观察程序崩溃。this
is not the pointer returned by thenew
at linechar* p = new char[s];
You can see that the sizes
there is bigger than 5Obj
instances. The difference (which should besizeof (std::size_t)
) is in additional memory, containing the length of the array, 5, immediately before the address contained inthis
.OK, the spec makes it clear:
http://sourcery.mentor.com/public/cxx-abi/abi.html#array-cookies
So, the virtual-ness of the destructor is irrelevant, what matters is that the destructor is non-trivial, which you can easily check, by deleting the keyword
virtual
in front of the destructor and observe the program crashing.根据 chills 的回答,如果你想让它“安全”:
Based on chills' answer, if you want to make it "safe":