好吧,表示漏看了LZ“常规”两个字。。
![[拍砖]](/static/emoticons/u62cdu7816.png)
那么看你底层是怎么搞到的了。。。
比如:
#include <cstdlib>
#include <new>
int main()
{
{
char* p = static_cast<char*>(std::malloc(1000));
char* q = new(p) char[1000/sizeof(char)]; // post-condition: q == p;
delete q; // equivilent to delete p, which is not resulted from a new-expression, causes undefined behavior;
}
{
char* p = new char[1000];
char* q = new(p) char[1000/sizeof(char)]; // post-condition: q == p;
delete q; // well-formed, equivalent to delete p at once, and both p and q become invalid pointers due to the call of deallocation function;
delete p; // double-deletion definitely leads to undefined behavior, for invalid pointer passed to deallocation function;
}
}