Placement new - application

E

elviin

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
 
J

Jacques Labuschagne

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

Peter Koch Larsen

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
 
R

red floyd

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

elviin

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

red floyd

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

CodeCracker

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

Peter Koch Larsen

CodeCracker said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top