c++ - Peculiar way for object creation? -


here example of implementing class creating objects:

template <class t> struct malloccreator {     static t* create() {         void* buf = std::malloc(sizeof(t));         if (!buf) return 0;         return new(buf) t;     } }; 

by using malloc , new operator.

what role of new , above syntax?

it's "placement new". code create raw memory size of 1 t malloc , construct new t in raw memory.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

php - mysql multiple keyword search in any order -

unity3d - Problems porting a GLSL shadertoy shader to unity -