Possible Duplicate:
C++'s “placement new”
Help with a c++ statement
I work in a product, where most of the modules have been written in C. Among them, one or two modules are written in C++. I find the below code in a C++ module, which I could not understand what is going on.
a = (char *) malloc (size);
b = new (a) MyClass();
Could someone explain me how a pointer allocated by malloc is used for new operator? Is it legitimate?
Thanks!
b = new (a) MyClass();is called placement new and it constructs new object of typeMyClasson a pre-allocated memory (memory that pointerapoints to).You should also check this question: What uses are there for "placement new"?