By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,708 Members | 1,529 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,708 IT Pros & Developers. It's quick & easy.

Placement new - application

P: n/a
Hello.
I tried a sample programm using placement new. I am not sure if i
understand this technique correctly. I do not mean usage but a real
world application. Could anyone to describe some applications where it
gives reason to use placement new? My doubts come from my consideration
that if I delete an object (e.g. 50MB) then system can allocate this
chunk of memory to another process and my process will not be able to
re-use this segment. This technique is good for preventing memory from
fragmentation. Because if I'm using the same segment of memory it
should be more sensible to keep this memory and use the same object and
just update it's members according to "streams" from outside. Can
anyone explain me the steps from new() to delete() to another
allocation with placement new()? Thank you.

Elviin Nela

Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
el****@gmail.com wrote:
Hello.
I tried a sample programm using placement new. I am not sure if i
understand this technique correctly. I do not mean usage but a real
world application. Could anyone to describe some applications where it
gives reason to use placement new?


How about for constructing objects in memory not allocated by malloc?
Say shared memory used for inter-process communication.

Jacques.
Jul 23 '05 #2

P: n/a

<el****@gmail.com> skrev i en meddelelse
news:11**********************@z14g2000cwz.googlegr oups.com...
Hello.
I tried a sample programm using placement new. I am not sure if i
understand this technique correctly. I do not mean usage but a real
world application. Could anyone to describe some applications where it
gives reason to use placement new? My doubts come from my consideration
that if I delete an object (e.g. 50MB) then system can allocate this
chunk of memory to another process and my process will not be able to
re-use this segment. This technique is good for preventing memory from
fragmentation. Because if I'm using the same segment of memory it
should be more sensible to keep this memory and use the same object and
just update it's members according to "streams" from outside. Can
anyone explain me the steps from new() to delete() to another
allocation with placement new()? Thank you.

Elviin Nela


While the standard operator new has two steps, namely to (1) allocate memory
and (2) create an object from that raw memory, the placement operator new
skips the first step, using the supplied memory to construct the object. It
is useful several places, e.g. when you implement a container (std::vector
almost certainly uses placement new).
Perhaps I'm stupid, but i do not see how this helps avoid memory
fragmentation. If you have the memory for your "expensive" object present
all the time, you could just avoid destroying the object until you finish
your program. In case you need to reinitialize the object, it would be a
better idea - in my opinion - to have a "reset" function available.

/Peter
Jul 23 '05 #3

P: n/a
el****@gmail.com wrote:
Hello.
I tried a sample programm using placement new. I am not sure if i
understand this technique correctly. I do not mean usage but a real
world application. Could anyone to describe some applications where it
gives reason to use placement new? My doubts come from my consideration
that if I delete an object (e.g. 50MB) then system can allocate this
chunk of memory to another process and my process will not be able to
re-use this segment. This technique is good for preventing memory from
fragmentation. Because if I'm using the same segment of memory it
should be more sensible to keep this memory and use the same object and
just update it's members according to "streams" from outside. Can
anyone explain me the steps from new() to delete() to another
allocation with placement new()? Thank you.

Elviin Nela


Memory mapped hardware in an embedded system.
Jul 23 '05 #4

P: n/a
I'd like to know how it is done when I call desctructor (after
placement new) so that the system keeps (??) the segment of memory for
my process until next placement new. Because I was somewhere reading
that it prevents memory from fragmentation - sorry I do not understand
this statement.

Jul 23 '05 #5

P: n/a
el****@gmail.com wrote:
I'd like to know how it is done when I call desctructor (after
placement new) so that the system keeps (??) the segment of memory for
my process until next placement new. Because I was somewhere reading
that it prevents memory from fragmentation - sorry I do not understand
this statement.


You don't call delete. You call the destructor directly.

T* p = new(placment);
// do stuff

p->~T();

// object is destroyed.
Jul 23 '05 #6

P: n/a
i'm sorry... I wanted to write dtor.

Jul 23 '05 #7

P: n/a


el****@gmail.com wrote:
i'm sorry... I wanted to write dtor.


Isn't it said that if you provide implementation of placement new you
also have to provide implementation of the placement delete which would
take care of returning the allocated block to the common pool
maintained by the application.

Jul 23 '05 #8

P: n/a

"CodeCracker" <sa********@gmail.com> skrev i en meddelelse
news:11**********************@g49g2000cwa.googlegr oups.com...


el****@gmail.com wrote:
i'm sorry... I wanted to write dtor.


Isn't it said that if you provide implementation of placement new you
also have to provide implementation of the placement delete which would
take care of returning the allocated block to the common pool
maintained by the application.

Nope. If you got the object via placement new, you would not normally delete
it. Your scenario looks more like you provided your own new operator in
which case you will also provide the delete operator.

/Peter
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.