C 64-bit Pointer Alignment

前端 未结 5 1040
野性不改
野性不改 2021-01-02 01:39

Are pointers on a 64-bit system still 4 byte aligned (similar to a double on a 32 bit system)? Or are they note 8 byte aligned?

For example, on a 64-bit system how

5条回答
  •  一个人的身影
    2021-01-02 02:16

    The language standard makes no statements about padding. The alignment rules are platform-specific (i.e., you have to align differently on e.g. a PowerPC CPU than on a x86_64 CPU), and they are implementation-defined, meaning your compiler can do whatever works (and might change that behaviour with different command-line options or after a version update).

    I strongly believe that any recommendation along the lines of "this is usually this or that" is misleading, and possibly dangerous.

    1. You could write a test program that executes a couple of sizeof() and/or offsetof()statements and writes a header for you containing some #defines stating the paddings used.

    2. You can use autoconf to do that for you.

    3. In the very least, you should add assert( sizeof( ... ) ) statements at the beginning of your main() function so you get informed when your assumptions are wrong.

提交回复
热议问题