2

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!

Community
  • 1
  • 1
Alexander
  • 450
  • 5
  • 16
  • 2
    possible duplicate of [Help with a c++ statement](http://stackoverflow.com/questions/7576303/help-with-a-c-statement), also [What is this second new?](http://stackoverflow.com/questions/3960548/what-is-this-second-new), also [Explanation of this new() statement](http://stackoverflow.com/questions/7847525/explanation-of-this-new-statement) – CB Bailey Feb 24 '12 at 09:57
  • great... I saw the opposite of this, explicit call to destructor, in STL code and was wondering what was that.. This is from xmemory.cc file of STL. template inline void _Construct(_Ty1 _FARQ *_Ptr) { // construct object at _Ptr with default value void _FARQ *_Vptr = _Ptr; ::new (_Vptr) _Ty1(); } // TEMPLATE FUNCTION _Destroy template inline void _Destroy(_Ty _FARQ *_Ptr) { // destroy object at _Ptr _Ptr->~_Ty(); } – PermanentGuest Feb 24 '12 at 10:03

3 Answers3

3

b = new (a) MyClass(); is called placement new and it constructs new object of type MyClass on a pre-allocated memory (memory that pointer a points to).

You should also check this question: What uses are there for "placement new"?

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
2

new can take parameter, which tells new the starting address of the memory...so what the code is doing is...

a = (char *) malloc (size);

allocate memory of the new class...

b = new (a) MyClass();

tell new to use 'a' as the starting address of memory, and initialize the clas at address of 'a' ...

P M
  • 837
  • 1
  • 10
  • 17
0

The syntax new (something) Type is called placement new. Like all new, it calls an allocator function, then the constructor of Type. The allocator function has the signature:

    void* operator new( size_t byteCount, something );

In other words, whatever you put in the parentheses is passed on as additional arguments to the operator new function.

In addition to the usual operator new function, the standard defines two other operator new functions, which take additional arguments:

void* operator new( size_t byteCount, std::nothrow_t ) noexcept;

and

void* operator new( size_t byteCount, void* where ) noexcept;

Note that both are noexcept. The first returns a null pointer if allocation fails (rather than throwing bad_alloc), and the second simply returns where. (The second is also known as placement new. Which leads to no end of confusion: “placement new” can refer to any extended new, or to jsut the one which takes a void*.)

In your case, the second is the one being used, although I would seriously question why: if the object type has a destructor, you will have to do:

b->~MyClass();
free( b );

to delete it. (Placement new and explicit delete are usually used in cases where you want to separate allocation from initialization. In an implementation of std::vector, for example, where the capacity can be larger than the size.) About the only reason for it here that I can see is that the object must be allocated in C++, but will be deleted in C (and of course, it has a trivial destructor—otherwise, there's no way C can correctly delete it).

James Kanze
  • 150,581
  • 18
  • 184
  • 329