As per my understanding, pointers on a 64 bit linux system are 8 bytes in size, and alignment requirement for these should be 8 byte aligned. If that's the case then why does new operator returns 16 byte (could be 32, but what matter is that this number is higher than 8) aligned pointer? Simple calls such as
struct alignas(8) SimpleChar
{
char c;
}
SimpleChar * c = new SimpleChar;
SimpleChar * c1 = new SimpleChar;
will allocate 32 bytes given each pointer is 16 bytes aligned. If instead I use placement new
SimpleChar * c = new (slab) SimpleChar;
SimpleChar * c1 = new (slab + 8) SimpleChar;
This will result into using 16 bytes for the two pointer types. My questions are as follows -
- Is 16 byte alignment of pointer return by operator new really necessary?
- Are there any side effects of using placement new and align every struct and pointer by 8 bytes? (One side effect I could think of is that this approach may use more memory as a struct might need only 4 byte alignment)
- I know we can define structs with higher alignment than 8 but if I am not doing so explicitly anywhere then is it safe to assume that any struct or pointer I use would have a maximum alignment of 8 bytes on a 64 bit linux system
Cheers, RG
EDIT: Instead of using char directly, I specified a struct to control the alignment.