question about placement new

Hi I was wondering in regards to the code below, every time I compile and run, the addresses of buffer and the address of the st1 structure I have allocated to buffer with placement new do not match. I was wondering why this is. I am quite new to C++ and programming as a whole, so I may be overlooking something stupidly simple, any assistance would be appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 #include <iostream>
#include <new>
using namespace std;

struct chaff
{
	char dross[20];
	int slag;
};

char buffer[sizeof(chaff)];

int main()
{ 
	
	cout << "Allocating st1 & st2 to Buffer" << endl;
	chaff * st1 = new (buffer) chaff;
	chaff * st2 = new (buffer + sizeof(char)) chaff;
	cout << "Address of Buffer : " << (void *) buffer << endl
		<< "Size of Buffer : " << sizeof(buffer) << " Bytes" << endl;
	cout << "Address of st1 : " << &st1 << endl;
	cout << "Address of st2 : " << &st2 << endl;
chaff * st2 = new (buffer + sizeof(char)) chaff;

this statement might not work due to allignment issues.

The fix of your problem:

cout << "Address of st1 : " << st1 << endl;
cout << "Address of st2 : " << st2 << endl;
That did the trick thanks for the help.
in regards to the alignment issue it would be better to use sizeof(chaff) instead of sizeof(char) yes?
Also when i was using &st1 to display address, what was the actual address that was being displayed using that operator then?
thanks
it would be better to use sizeof(chaff) instead of sizeof(char) yes?
It would be correct. Right now you have overlapping objects which leads to UB and you cannot complain if your PC will suddenly decide to fly away to warm countries.

However you still have buffer overrun problem (st2 will not fit in buffer made for one chaff object) and you still have aligment issue as it is not guaranteed that buffer is aligned properly

Proper solution: char alignas(chaff) buffer[sizeof(chaff) * 2]; //

Also when i was using &st1 to display address, what was the actual address that was being displayed using that operator then?
You requested address of pointer s1, you got it. You probably wanted address of object which s1 points to, but you never asked for that.

Last edited on
Cool that that all makes sense, thanks a lot for the help guys, im still quite new to this so have a lot to learn yet
The pointers contain addresses, so getting the value of the pointer will give you the address it contains. As in:

st1

Some people prefer to make it more explicit that st1 is a pointer. In that case, you could write:

&*st1

The operators & and * effectively cancel each other out, so this expression is equivalent to the previous one, except that in this case the variable st1 has to be a pointer, otherwise the compiler will emmit an error. This expression is preffered by people who are sometimes unsure. If you find your self being unsure too often, it is recommended to visit a psychologist.

However, there are some people who opine that the previous expression is not sufficiently verbose. To increase the verbosity, this expression can be used:

&*&*&*&*&*st1

Those feople might also appreciate the information that the Center for Research of Mental Impairment (CRMI) is currently looking for new candidates. Feel free to contact them at candidates@mentalimpairment.org .

:)
Last edited on
Topic archived. No new replies allowed.