C 64-bit Pointer Alignment C 64-bit Pointer Alignment c c

C 64-bit Pointer Alignment


Data alignment and packing are implementation specific, and can be usually changed from compiler settings (or even with pragmas).

However assuming you're using default settings, on most (if not all) compilers the structure should end up being 16 bytes total. The reason is because computers reads a data chunk with size of its native word size (which is 8 bytes in 64-bit system). If it were to pad it to 4 byte offsets, the next structure would not be properly padded to 64-bit boundary. For example in case of a arr[2], the second element of the array would start at 12-byte offset, which isn't at the native byte boundary of the machine.


I don't think you can rely on any hard-and-fast rules. I think it's a function of the compiler you use and the compilation options you choose.

Your best bet is to write a program that tests this and spits out a header file that codifies the alignment rules as #defines. You might also be able to just calculate what you're interested in right in the macros, too.


Generally on a 64-bit system:

struct a {    void* ptr;      // size is 8 bytes, alignment is 8    char myChar;    // size is 1 byte,  alignment is 1                    // padding of 7 bytes so array elements will be properly aligned}

For a total size of 16 bytes.

But this is all implementation defined - I'm just giving an example that likely to be true for many (most?) 64-bit systems.