Find holes in C structs due to alignment

后端 未结 7 2578
难免孤独
难免孤独 2021-02-20 18:26

Is there a way in gcc or clang (or any other compiler) to spit information about whether a struct has holes (memory alignment - wise) in it ?

Thank you.

ps: If

7条回答
  •  日久生厌
    2021-02-20 18:58

    I Don't know any automatic tool, but this could be helpful example:

    #include 
    
    struct test {
      typea a;
      typeb b;
      typec c;
    };
    
    int gapB = offsetof(struct test, b) - (offsetof(struct test, a) + sizeof(typea));
    int gapC = offsetof(struct test, c) - (offsetof(struct test, b) + sizeof(typeb));
    
    printf("Gap of b:%d/n", gapB);
    printf("Gap of c:%d/n", gapC);
    

    *Note: you will have to do so for each two members in your stuck.

提交回复
热议问题