472,354 Members | 1,230 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

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 1789
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Giancarlo Niccolai | last post by:
Hello all. I have peeked through the FAQ and all relevant links, and also through Stroustrup book, but I have not been able to find an answer, so I have to post here as a last resort. It...
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
14
by: Kevin | last post by:
A couple of easy questions here hopefully. I've been working on two different database projects which make use of multiple forms. 1. Where's the best/recommended placement for command buttons...
4
by: sreedhar.cs | last post by:
Hi all, In my application,I want to place a vector in a specific location in shared memory.(a user supplied pointer). I understand that the STL allocator mechanism places the data objects within...
15
by: mangesh | last post by:
This code is from c++ faq in section 11 : void someCode() { char memory; void* p = memory; Fred* f = new(p) Fred(); f->~Fred(); // Explicitly call the destructor for the placed object }
13
by: Samshayam | last post by:
I have come across the application of placement new in memory mapped i/o in a number of books.I am not able to understand it completely, may be becaues of my lack of knowledge with memory mapped...
5
by: Lagarde Sébastien | last post by:
Hello, I write code to debug new call with following macro: #define new (MemoryManager::Get().setOwner (__FILE__, __LINE__, _FUNCTION-), FALSE) ? NULL : new The setOwner allow to save the...
15
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
9
by: karthikbalaguru | last post by:
Hi, I find that articles stating that 'placement new' constructs an object on a pre-allocated buffer and so takes less time. Actually, we have to consider the allocation of the buffer and then...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.