sizeof for extern struct

Discussion in 'C Programming' started by CptDondo, Sep 14, 2007.

  1. CptDondo

    CptDondo Guest

    I've got a question on the proper handling of sizeof.

    I have two files; a.c and b.c

    In b.c I have:

    struct Key en_keys[] = {
    {EN_ENTER, DELAY, NULL},
    };

    in a.c I have

    extern struct Key en_keys[];

    and later, in a.c I do

    foo = sizeof(en_keys);

    Of course the compiler promptly pukes since sizeof is computed at
    compile time and it doesn't know the size until link time....

    So - what's the proper / recommended / sensible way to deal with this?
    The number of elements in en_keys[] could change and I really don't want
    to have to redo all of the size info.
     
    CptDondo, Sep 14, 2007
    #1
    1. Advertising

  2. CptDondo

    Punkie Guest

    afaik you cant have the sizeof value at compile time because it is in
    another code unit.

    If you dont really need the exact value at compile time:
    b.c
    int theSize = sizeof(en_keys);
    a.c
    extern int theSize;
    Typically you dont use the value, unless you use it for eg template
    parameter instantiation..


    Or duplicate the struct def in a.c
    As long as they are idential it should be fine. Just dont try to compare the
    types.
     
    Punkie, Sep 14, 2007
    #2
    1. Advertising

  3. CptDondo

    Ian Collins Guest

    CptDondo wrote:
    > I've got a question on the proper handling of sizeof.
    >
    > I have two files; a.c and b.c
    >
    > In b.c I have:
    >
    > struct Key en_keys[] = {
    > {EN_ENTER, DELAY, NULL},
    > };
    >
    > in a.c I have
    >
    > extern struct Key en_keys[];
    >
    > and later, in a.c I do
    >
    > foo = sizeof(en_keys);
    >
    > Of course the compiler promptly pukes since sizeof is computed at
    > compile time and it doesn't know the size until link time....
    >
    > So - what's the proper / recommended / sensible way to deal with this?
    > The number of elements in en_keys[] could change and I really don't want
    > to have to redo all of the size info.


    Either add an extern const variable for the number of keys, or if the
    the array is built at run time, end it with a NULL, as you appear to be
    doing.

    --
    Ian Collins.
     
    Ian Collins, Sep 14, 2007
    #3
  4. CptDondo

    Ian Collins Guest

    Punkie wrote:

    Please retain the context of the thread you are replying to.

    > afaik you cant have the sizeof value at compile time because it is in
    > another code unit.
    >
    > If you dont really need the exact value at compile time:
    > b.c
    > int theSize = sizeof(en_keys);


    The type of sizeof is size_t.

    > a.c
    > extern int theSize;
    > Typically you dont use the value, unless you use it for eg template
    > parameter instantiation..
    >

    This is C....

    >
    > Or duplicate the struct def in a.c
    > As long as they are idential it should be fine. Just dont try to compare the
    > types.


    Not a good idea, one then has to maintain two or more copies of the same
    array.

    --
    Ian Collins.
     
    Ian Collins, Sep 14, 2007
    #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. Thomas Matthews
    Replies:
    5
    Views:
    2,523
    tom_usenet
    Aug 2, 2004
  2. Derek
    Replies:
    7
    Views:
    24,395
    Ron Natalie
    Oct 14, 2004
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,433
    Chris Fogelklou
    Apr 20, 2004
  4. Kieran Simkin

    sizeof extern arrays

    Kieran Simkin, Jun 4, 2004, in forum: C Programming
    Replies:
    7
    Views:
    4,226
    Dan Pop
    Jun 7, 2004
  5. Andre
    Replies:
    5
    Views:
    558
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page