Why is the “alignment” the same on 32-bit and 64-bit systems?

后端 未结 4 1559
暗喜
暗喜 2021-02-19 00:16

I was wondering whether the compiler would use different padding on 32-bit and 64-bit systems, so I wrote the code below in a simple VS2019 C++ console project:

         


        
4条回答
  •  轮回少年
    2021-02-19 01:04

    The padding is not determined by the word size, but by the alignment of each data type.

    In most cases, the alignment requirement is equal to the type's size. So for a 64 bit type like int64 you will get an 8 byte (64 bit) alignment. Padding needs to be inserted into the struct to make sure that the storage for the type ends up at an address that is properly aligned.

    You may see a difference in padding between 32 bit and 64 bit when using built-in datatypes that have different sizes on both architectures, for instance pointer types (int*).

提交回复
热议问题