Re: parsing C files to determine the size of structures

Discussion in 'C Programming' started by Fred, May 11, 2009.

  1. Fred

    Fred Guest

    On May 11, 10:51 am, arne <> wrote:
    > Hello,
    >
    > I am working on a project in which i need to parse a c-header with structure and variable
    > definitions to determine the size of the structures.


    Why? You give us your proposed solution to a problem that is unstated.
    If we know why you think you need to do this, we might be able to make
    some suggestions.

    >
    > I thought of a module which creates as output a list like:
    >
    > {
    > var_or_struct_name[64],u32 size,
    > var_or_struct_name[64],u32 size,
    > ....
    >
    > }
    >
    > Is I possible to use a c-compiler in some way, or using part of a open source compiler to do that?
    >
    > Any Ideas ?
    >


    You do realize that the size of a structure will depend on which
    compiler is used?

    And a well-written c program is unlikely to have any variables (not in
    a structure) defined in a header file.
    --
    Fred K
    Fred, May 11, 2009
    #1
    1. Advertising

  2. arne <> writes:
    [...]
    >> You do realize that the size of a structure will depend on which
    >> compiler is used?


    Fred <> wrote the above. Please don't
    delete attribution lines.

    > not with #pragma pack(push,1) which is necessary for the
    > communication objects.


    Even with a non-standard pragma like that, the sizes of the
    fundamental types can vary from one compiler to another.
    For example, int might be 2 bytes on one system, 4 bytes on another.

    If you're assuming certain sizes, that might be ok, but be aware that
    your assumptions could break on a different compiler.

    [...]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 11, 2009
    #2
    1. Advertising

  3. Fred

    jameskuyper Guest

    arne wrote:
    ....
    > > You do realize that the size of a structure will depend on which
    > > compiler is used?

    > not with #pragma pack(push,1) which is necessary for the communication objects.


    The format of a pragma that forces padding can be different for
    different compilers, and there's no guarantee that any such pragma
    exists. The only #pragmas that are portable are those starting with
    STDC, and those pragmas were added in C99, they can't be relied if
    you're using C90.

    In any event, preventing structure padding is only the beginning of
    the issue. The only standard C types whose size in bits can be relied
    upon are the exact-sized types declared in <stdint.h> that were also
    added in C99, and those types aren't guaranteed to be supported.

    In another message you mentioned that the only types involved were:

    > unsigned char = u8, unsigned short = u16 and unsigned long = u32


    Are you aware of the fact that unsigned char is not required to be 8
    bits, unsigned short is not required to be 16 bits, and unsigned long
    is not required to be 32 bits?
    jameskuyper, May 11, 2009
    #3
  4. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1



    Fred wrote:
    > On May 11, 10:51 am, arne <> wrote:
    >> Hello,
    >>
    >> I am working on a project in which i need to parse a c-header with structure and variable
    >> definitions to determine the size of the structures.

    >
    > Why? You give us your proposed solution to a problem that is unstated.
    > If we know why you think you need to do this, we might be able to make
    > some suggestions.
    >
    >> I thought of a module which creates as output a list like:
    >>
    >> {
    >> var_or_struct_name[64],u32 size,
    >> var_or_struct_name[64],u32 size,
    >> ....
    >>
    >> }
    >>
    >> Is I possible to use a c-compiler in some way, or using part of a open source compiler to do that?
    >>
    >> Any Ideas ?
    >>

    >
    > You do realize that the size of a structure will depend on which
    > compiler is used?
    >
    > And a well-written c program is unlikely to have any variables (not in
    > a structure) defined in a header file.


    With the exception of global constants you do not want to substitute.

    > --
    > Fred K


    - --
    - --Falcon Darkstar Christopher Momot
    - --
    - --OpenPGP: (7902:4457) 9282:A431

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.9 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iEYEARECAAYFAkoJN5gACgkQeQJEV5KCpDGLawCdFjqvKZ9+Wz+0++TbtirULTRE
    PIkAnRyymUcJPrMBqzXKqaSfY9hNAZNU
    =td1i
    -----END PGP SIGNATURE-----
    Falcon Kirtaran, May 12, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tweak
    Replies:
    14
    Views:
    2,776
    Eric Sosman
    Jun 11, 2004
  2. Alfonso Morra
    Replies:
    11
    Views:
    709
    Emmanuel Delahaye
    Sep 24, 2005
  3. sunny
    Replies:
    5
    Views:
    2,108
    CBFalconer
    Aug 17, 2006
  4. Peña, Botp
    Replies:
    1
    Views:
    229
    Robert Klemme
    Jan 24, 2004
  5. Timo
    Replies:
    1
    Views:
    192
    Vincent van Beveren
    Jun 14, 2004
Loading...

Share This Page