470,641 Members | 1,521 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,641 developers. It's quick & easy.

Placement new - application

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
8 1709
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

<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
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
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
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
i'm sorry... I wanted to write dtor.

Jul 23 '05 #7


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

"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.

Similar topics

23 posts views Thread by Giancarlo Niccolai | last post: by
20 posts views Thread by Ioannis Vranos | last post: by
4 posts views Thread by sreedhar.cs | last post: by
13 posts views Thread by Samshayam | last post: by
5 posts views Thread by Lagarde Sťbastien | last post: by
15 posts views Thread by LuB | last post: by
9 posts views Thread by karthikbalaguru | last post: by
1 post views Thread by Korara | last post: by
???
1 post views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.