Find holes in C structs due to alignment

后端 未结 7 2344
难免孤独
难免孤独 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 19:03

    Gimpel's FlexeLint/PClint can do this.

    $ cat tst.c
    int main (void)
    {
        struct {
            char c;
            double d;
            short s;
        } f = { 1, 2.0, 3 };
    
        return f.c;
    }
    

    It will report

    $ flexelint -w1 +e95? tst.c
    FlexeLint for C/C++ (Unix) Vers. 9.00L, Copyright Gimpel Software 1985-2014
    
    --- Module:   tst.c (C)
                    _
            double d;
    tst.c  5  Note 958: Padding of 7 byte(s) is required to align member on 8 byte
        boundary
        _
        } f = { 1, 2.0, 3 };
    tst.c  7  Note 959: Nominal struct size (18 bytes) is not an even multiple of
        the maximum member alignment (8 bytes)
    tst.c  7  Note 958: Padding of 6 byte(s) is required to align end of struct on
        8 byte boundary
    
    0 讨论(0)
提交回复
热议问题