You are not logged in.

#1 2010-03-24 15:27:04

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 876

Unusual "new" syntax in C++

I'm reading a piece of C++ code, and I found an unusual syntax for the new operator:

new (ptr) MyClass();

where ptr is a pointer and MyClass() the default constructor of a certain class. I searched the web to find what this is supposed to do, but I found nothing. My guess is that new creates a MyClass instance at the memory address specified by ptr; in other words, that no memory allocation is done since ptr has to point to an already allocated memory bunch. Am I right?
Thank you

Offline

#2 2010-03-24 15:42:43

ber_t
Member
From: Berlin, Germany
Registered: 2010-03-10
Posts: 214
Website

Re: Unusual "new" syntax in C++

Your guess is correct.
It's called "placement new". More info under http://www.devx.com/tips/Tip/12582

Offline

#3 2010-03-24 15:46:43

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 876

Re: Unusual "new" syntax in C++

Thank you very much, ber_t. So I guess that there's no check on the fact that objects put in place can fit inside the allocated memory? In other words, if I allocate space for 10 chars and then do a placement new for a string with 20 chars what would happen? A segmentation violation or something similar?

Offline

#4 2010-03-24 17:44:19

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: Unusual "new" syntax in C++

Yeah you'd get undefined behaviour, so don't do that smile.  In fact, there's no way it could check, 'cause the pointer you give it could be from malloc(), on the stack, in a global variable, or an mmap'ed file, etc.

The other important thing to know is that there's no "placement delete" syntax, so you can't do "delete (ptr) ptr;" or anything similar.  Instead, you call the destructor directly, i.e. "ptr->~MyClass();", and this is basically the only time you'll ever do that.

Offline

#5 2010-03-24 17:50:22

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 876

Re: Unusual "new" syntax in C++

tavianator wrote:

The other important thing to know is that there's no "placement delete" syntax, so you can't do "delete (ptr) ptr;" or anything similar.  Instead, you call the destructor directly, i.e. "ptr->~MyClass();", and this is basically the only time you'll ever do that.

Thanks, I didn't think about this!
Could I, for example, do this if I want also to free the memory:

MyClass *dummyPtr = ptr;
delete dummyPtr;
dummyPtr = ptr = NULL;

This way delete should be aware that the object at memory location given by ptr is of MyClass type, and should correctly destroy it and free the memory. This obviously if the original allocated area size was sizeof(MyClass)...

Offline

#6 2010-03-24 22:34:39

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: Unusual "new" syntax in C++

Nope you couldn't do that, because "delete" only works on memory allocated by the regular (non-placement) operator new.  So if it was allocated by malloc(), mmap(), or something else, delete will do the wrong thing.  If the memory was originally allocated by operator new(), then that would work, but be careful:

char *ptr = new char[sizeof(MyClass)];
MyClass *dummyPtr = new(static_cast<void*>(ptr)) MyClass();
delete dummyPtr;

won't work because ptr was allocated with operator new[], which needs to be matched by an operator delete[] (on a pointer to type with the same size as the objects in the initial allocation, too).

Offline

#7 2010-03-25 07:20:31

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 876

Re: Unusual "new" syntax in C++

Ok, thanks a lot. I think I understand the point, now.

Offline

Board footer

Powered by FluxBB