C风格new操作符malloc,如何调用构造函数
keywords: C风格new操作符malloc,如何调用构造函数
keywords:C++, new, placement new
You’ll need to use placement new after getting the raw memory from malloc.
void* mem = malloc(sizeof(S));
S* s = new (mem) S(); //this is the so called "placement new"
When you’re done with the object you have to make sure to explicitly call its destructor.
s->~S();
free(mem);