Jump to content
  • Advertisement
Sign in to follow this  
Khatharr

Placement new

This topic is 2134 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Obviously I'm missing something here, so I'll post in the beginner's forum.

 

Query:

 

Why would I ever use placement new?

Share this post


Link to post
Share on other sites
Advertisement

So if the ctor is called manually on a derived class it doesn't set up the vtable?

 

Edit:

 

Er... This thing isn't letting me call the ctor manually. Is that new or am I remembering incorrectly?

Edited by Khatharr

Share this post


Link to post
Share on other sites

Placement new is what you would write to call the constructor manually for some memory you provide, if you would write only a constructor call it would mean something else like making a temporary or casting a value.

Btw. if one doesnt need normal new and placement new at same time for a single class one can also overwrite operator new and delete for that class and then new and delete it normally, without needing the weirdness of placement new and direct destructor calls.

Share this post


Link to post
Share on other sites

As an example of a usage of placement new:

 

I create a placement new pointer to shared memory (on Linux systems).  Shared memory, depending on implementation, can actually be placed directly in /dev/shm as a RAM disk.  If you need two complete processes to share information it's one way of doing things.

 

IIRC I've seen implementations that use placement new on things like internal timers.  As Hodgman points out, this is almost never a concern for high level programmers. 

 

Edit:  Clarifying the implementation of the RAM disk on Linux.

Edited by Cosmic314

Share this post


Link to post
Share on other sites
Besides the above, placement new is the only way to initialize non-POD union members. (POD unions can be used without initialization, by just using assignment.)

Share this post


Link to post
Share on other sites

It can also be used (with care) to initialize memory-mapped I/O devices if you want a direct object representation of the memory that controls it. Sometimes its better to have an object representation which is a proxy for the object, but sometimes not. You can use placement new with careful construction in the latter case. I say careful because you have to make sure your class's data members line up with the in-memory registers, and you also have to initialize some of those registers in the manner and order that the hardware requires -- but its a neat parlor trick when the opportunity arises.

Share this post


Link to post
Share on other sites

It can also be used (with care) to initialize memory-mapped I/O devices if you want a direct object representation of the memory that controls it. Sometimes its better to have an object representation which is a proxy for the object, but sometimes not. You can use placement new with careful construction in the latter case. I say careful because you have to make sure your class's data members line up with the in-memory registers, and you also have to initialize some of those registers in the manner and order that the hardware requires -- but its a neat parlor trick when the opportunity arises.

 

Aye. I was wondering about that earlier. I've never really worked with such a device, but I was thinking that maybe that was the reasoning behind it.

Share this post


Link to post
Share on other sites

It can work, but I don't think I'd go as far as saying it was a critical part of the reasoning behind it. Enabling custom allocations like others have described is a damn fine reason all on its own. And like I said, you first have to ensure your data aligns to the registers, and then have to possibly initialize those registers in the correct order, and sometimes going back to write new values to them mid-way-through, in order to actually bring up the hardware device correctly. In C++ order of initialization by default is related to the order in which member variables are declared, so C++ is somewhat at odds with the needs of some hardware. You can work around it, of course, by having an init member-function but that negates some of the utility of classes. Its sometimes more practical to just write a macro that dereferences the right address and casts to a class representing the type, with a free-standing init function.

 

But definitely memory-mapped IO was on their minds in general, that's actually what bit-fields are for -- they're not meant so that you can pack your data (if they were, they're a half-baked solution for that).

Share this post


Link to post
Share on other sites

Note that while scalar (regular) placement new is useful, the array form is more or less useless. It won't reliably place the first object in the array at the address you give it, which pretty much defeats the purpose of using placement new in the first place.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!