memory alignment __attribute__( ( aligned ( 8 ) ) )

前端 未结 3 1446
死守一世寂寞
死守一世寂寞 2020-12-30 06:21

I got a program in a book

#include 
int main( )
{
    struct data
    {
        int a    __attribute__( ( aligned ( 8 ) ) ) ;
        char ch          


        
3条回答
  •  一向
    一向 (楼主)
    2020-12-30 07:17

    The aligned(N) attribute aligns the current data item on an address which is a multiple of N, by inserting padding bytes before the data item. You appear to expect that it does something with packing, which is different.

    Your structure appears to be laid out as follows:

    Address  Member
    -------  -------
    2280712  a        [address aligned on multiple of 8]
    2280713  a
    2280714  a
    2280715  a
    2280716  ch       [address aligned on multiple of 1]
    2280717  (unused)
    2280718  (unused)
    2280719  (unused)
    2280720  s        [address aligned on multiple of 4]
    2280721  s
    2280722  s
    2280723  s
    

提交回复
热议问题