sizeof and placement new

Hello, in the following code I am confused about what is going on in line 41:
pd2 = new (buffer + N * sizeof(double)) double[N];
I cannot understand what sizeof is supposed to be calculating.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  //Listing 9.10 newplace.cpp		
// newplace.cpp -- using placement new		
#include <iostream>		
#include <new> // for placement new		
const int BUF = 512;		
const int N = 5;		
char buffer[BUF]; // chunk of memory		
int main()		
{		
	using namespace std;	
		double *pd1, *pd2;
	int i;	
	cout << "Calling new and placement new:\n";	
	pd1 = new double[N]; // use heap	
	pd2 = new (buffer) double[N]; // use buffer array	
	for (i = 0; i < N; i++)	
		pd2[i] = pd1[i] = 1000 + 20.0 * i;
	cout << "Memory addresses:\n" << " heap: " << pd1	
		<< " static: " << (void *)buffer << endl;
	cout << "Memory contents:\n";	
	for (i = 0; i < N; i++)	
	{	
		cout << pd1[i] << " at " << &pd1[i] << "; ";
		cout << pd2[i] << " at " << &pd2[i] << endl;
	}	
	cout << "\nCalling new and placement new a second time:\n";	
	double *pd3, *pd4;	
	pd3 = new double[N]; // find new address	
	pd4 = new (buffer) double[N]; // overwrite old data	
	for (i = 0; i < N; i++)	
		pd4[i] = pd3[i] = 1000 + 40.0 * i;
	cout << "Memory contents:\n";	
	for (i = 0; i < N; i++)	
	{	
		cout << pd3[i] << " at " << &pd3[i] << "; ";
		cout << pd4[i] << " at " << &pd4[i] << endl;
	}	
	cout << "\nCalling new and placement new a third time:\n";	
	delete[] pd1;	
	pd1 = new double[N];	
	pd2 = new (buffer + N * sizeof(double)) double[N];	
	for (i = 0; i < N; i++)	
		pd2[i] = pd1[i] = 1000 + 60.0 * i;
	cout << "Memory contents:\n";	
	for (i = 0; i < N; i++)	
	{	
		cout << pd1[i] << " at " << &pd1[i] << "; ";
		cout << pd2[i] << " at " << &pd2[i] << endl;
	}	
	delete[] pd1;	
	delete[] pd3;	
	return 0;	
}		
Last edited on
sizeof() is a function that returns the amount of memory used by its input. In this case, the code is asking for the amount of memory a standard double requires. This function is important for direct memory allocation, a key feature of C/C++. For example: In the line of code it's being used in, it seems that this code is allocating memory for a buffer to store double values before being output to cout.
Last edited on
shOes wrote:
It seems that this code is allocating memory for a buffer to store double values before being output to cout.

No. Line 41 is a placement new. This does not actually allocate any memory. The expression in the () calculates an address based on the address of buffer at line 7 and adds the size of N doubles to the base address.
Last edited on
No. Line 41 is a placement new. This does not actually allocate any memory. The expression in the () calculates an address based on the address of buffer at line 7 and adds the size of N doubles to the base address.

Oh my mistake. Pointer arithmetic is even more esoteric to me.
Thank you very much for your help.
Topic archived. No new replies allowed.