A way to find the size and location of padding in a struct?

前端 未结 10 2471
南笙
南笙 2020-12-16 02:49

I\'m trying to write a tool that will take as input some C code containing structs. It will compile the code, then find and output the size and offset of any padding the com

10条回答
  •  抹茶落季
    2020-12-16 03:21

    You could use Exuberant Ctags to parse your source files instead of using a CPAN module or hacking something up yourself. For instance, for the following code:

    typedef struct _foo {
        int a;
        int b;
    } foo;
    

    ctags emits the following:

    _foo    x.c     /^typedef struct _foo {$/;"     s                               file:
    a       x.c     /^    int a;$/;"                m       struct:_foo             file:
    b       x.c     /^    int b;$/;"                m       struct:_foo             file:
    foo     x.c     /^} foo;$/;"                    t       typeref:struct:_foo     file:
    

    The first, fourth, and fifth columns should be enough for you to determine what struct types exist and what their members are. You could use that information to generate a C program that determines how much padding each struct type has.

提交回复
热议问题