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
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
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 .