According to https://isocpp.org/wiki/faq/dtors#placement-new the address passed into placement-new has to be properly aligned. But the example it gives seems to contradict that.
char memory[sizeof(Fred)];
This buffer is most likely not aligned for Fred, since it's a dumb char[]
, so memory
can point to pretty much anywhere. Then it does placement-new on this address.
Is the example contradicting the alignment requirement it says in the DANGER footnote?
That leads to a related question:
How can I create a buffer (either stack or heap) that is aligned for a type T
(to use in placement-new of one or more T objects)?
By buffer I mean a char[]
or void*
buffer of some size, not a T[]
because that would be object allocation which defeats the point of doing placement-new afterwards.
Thank you.
Fred
,sizeof(Fred)
may be 0 or 1. – Laurentiansizeof
cannot be 0 as per the Standard. – Katiekatina