Kakalokia
Kakalokia

Reputation: 3191

C++ placement new

Sorry if this question will sound stupid, but I'm just starting to learn C++ and there is something confusing me about the placement new

I've been reading C++ Primer (which I find is a very good book to learn C++), and in the placement new section there is an example given. The example uses a char array to provide memory space for the placement new

const int BUF = 512;
const int N = 5;
char buffer[BUF];
double * pd1;
pd1 = new (buffer) double[N];

My question is why is it using a char array to provide memory space for the placement new? Also the last line in the code above is allocating memory for an array of double, how is that possible when the original memory space contains a char array? If the placement new is using the memory space of the char array, does this mean when we allocate the double array it overwrites the char array in that memory?

Again sorry if the question is strange, but hope I've made it quite clear.

Upvotes: 6

Views: 1471

Answers (5)

Olaf Dietsche
Olaf Dietsche

Reputation: 74098

char buffer[BUF]; is just some memory. There's no type information attached to the bytes composing buffer. Only the compiler knows, that this memory region is supposed to hold characters. You could use any type, even double:

double buffer[BUF];
double *pd1 = new (buffer) double[N];

Upvotes: 0

Nicol Bolas
Nicol Bolas

Reputation: 474336

why is it using a char array to provide memory space for the placement new?

Why not? char is the smallest type that C++ defines, and on virtually every implementation, it is one byte in size. Therefore, it makes a good type to use when you need to allocate a block of memory of a certain size.

C++ also has very specific mechanics about how arrays of char (and only char are allocated. A new char[*], for example, will not be aligned to the alignment of char. It will be aligned to the maximum normal alignment for any type. Thus, you could use it to allocate memory and then construct any type into that memory.

Also the last line in the code above is allocating memory for an array of double, how is that possible when the original memory space contains a char array?

It is not allocating anything. It is constructing an array, using the memory you have given it. That's what placement new does, it constructs an object in the memory provided.

If the placement new is using the memory space of the char array, does this mean when we allocate the double array it overwrites the char array in that memory?

Yes.

Upvotes: 7

pts
pts

Reputation: 87401

Yes, the char array and the double array would overlap, more specifically they would start at the same address in memory, i.e. (long)buffer and (long)pd1 would be the same. We can emphasize the overlap even more by making the byte sizes match (assuming sizeof(char) == 1):

const int N = 5;
char buffer[N * sizeof(double)];
double *pd1 = new (buffer) double[N];

Yes, if you modify the data pd1 points to, then the data buffer points to would also be modified. And the other way round as well. (See also the GCC flag -fstrict-aliasing to learn about how compiler optimizations work with such an overlap.)

Upvotes: 3

brewbuck
brewbuck

Reputation: 897

Memory is memory. The machine doesn't care what type of data is stored there, that's up to the language to define and enforce. The answer to "why" is "because C++ is designed to let you."

Upvotes: 1

imreal
imreal

Reputation: 10378

There are no stupid questions.

It uses char probably because it makes you think about raw bytes (char is usually 1 byte long). The last line on the code is not allocating memory, it just places a double array over the mentioned buffer. If it was an object, it would also call the constructor. Yes, the char array gets overwritten.

Upvotes: 2

Related Questions