Very confused on Placement New.

I understand normal new and know how it works, but for some reason placement new is puzzling me. I have a slight grasp on how it works. You just include the <new> header file and use new with the argument of where the memory should be located. One thing I don't get is how you decide what to send as the argument. Do you just do the math on the max amount of memory you would need for the use of new? Or just a rough estimate or is there maybe something simple I'm missing?

Also, I just don't get when it's useful. Is there maybe a good tutorial explaining it or could someone provide a real life example of when they found it was needed?
Now new shouldn't require a header, it is a primitive of C++. (Unless Microsoft decided to add this and you're using VS)

It basically creates an object in memory and returns a pointer to it;

1
2
3
4
5
6
7
8
9
10
11
12
13
int* pinteger = new int[10];

class Object
{
   Object(int);  //Constructor
};

{
   Object object(5);
   Object* pobject = new Object(5);
}

//object has been destroyed, the address pobject is pointing to remains, the pointer is destroyed 


When you get into the pointer world it will prove invaluable, some things in C++ only make sense when you need them.
Last edited on
Placement new has a couple of uses, all of which are fairly advanced topics. Here's two.

1. Supposed I have a register on a PCI card memory mapped into my address space at memory location 0x1000. Suppose further I want to create a C++ object that I can use to read and write the bits of the register. One way to do this is to pass the address of the register to constructor and have the object store it. But this is somewhat expensive, in that to read the register now requires two operations: first, I must read my data member to get the address of the register, then I must read the value stored there. I can optimize this by having my object contain only a single data member whose address is already 0x1000. Then I can just read the register without the extra memory access. To do this, I have to force my object to be allocated at memory location 0x1000. How? Placement new.

2. Let's think about std::vector<>. std::vector<> likes to allocate more memory than what it needs to store the number of elements it needs to hold. So for example, if you have a std::vector<> with 10 elements, it might internally have enough storage for 16. How it does allocate "extra" memory? One way would be to just do: new T[16]; to create 16 of your T's. The problem? It runs T's default constructor 16 times, even though you only have 10 elements. To avoid this, std::vector<T> actually does new char[16*sizeof(T)] (roughly speaking). That is, it allocates a char array instead. The char array contains enough memory to hold 16 T's. How, then, does std::vector<T>::insert( const T& elem ) actually work? std::vector<> figures out the memory location in that char array where T should be inserted, then uses placement new on that address to copy construct your T.


Thank you for the replies, makes much more sense now. Also, @ Turbine, I'm talking about placement new, not normal new. Placement new does require you to include the new header file (unless my textbook is completely wrong (Which would be very, very bad!! :( ), but I don't think it is).
Topic archived. No new replies allowed.