The size of structs containing "int a[0]"?

Discussion in 'C Programming' started by Lokicer, Oct 16, 2005.

  1. Lokicer

    Lokicer Guest

    Hi, i am a c newbie, i write some code to get size of structs
    i compile and run it in VC6.0

    //#pragma pack(1)

    typedef struct tag_NullMsg
    {
    int a[0];
    } tNullMsg;
    typedef struct tag_CharMsg
    {
    char c;
    int a[0];
    } tCharMsg;

    //#pragma pack()

    int main(int arg)
    {
    printf("size of tNullMsg is %d\n", sizeof(tNullMsg));
    printf("size of tCharMsg is %d\n", sizeof(tCharMsg));
    }

    when i open #pragma pack(1) switch the size of tNullMsg is 1 and tCharMsg is
    1
    when i close #pragma pack(1) switch the size of tNullMsg is 1 and tCharMsg
    is 4

    who can explain it ?
    thanks very much!

    Lokicer
     
    Lokicer, Oct 16, 2005
    #1
    1. Advertising

  2. Lokicer <> wrote:

    > typedef struct tag_NullMsg
    > {
    > int a[0];
    > } tNullMsg;


    Attempting to use a zero-length array is a constraint violation
    requiring a diagnostic. Everything after that is outside the scope of
    the Standard (both C89 and C99, I believe).

    > //#pragma pack()


    > int main(int arg)
    > {
    > printf("size of tNullMsg is %d\n", sizeof(tNullMsg));
    > printf("size of tCharMsg is %d\n", sizeof(tCharMsg));
    > }


    > who can explain it ?
    > thanks very much!


    People who understand the use of your implementation - seek another
    newsgroup for that.

    http://www.ungerhu.com/jxh/clc.welcome.txt
    http://www.eskimo.com/~scs/C-faq/top.html
    http://benpfaff.org/writings/clc/off-topic.html

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 16, 2005
    #2
    1. Advertising

  3. Christopher Benson-Manica <> writes:
    > Lokicer <> wrote:
    >> typedef struct tag_NullMsg
    >> {
    >> int a[0];
    >> } tNullMsg;

    >
    > Attempting to use a zero-length array is a constraint violation
    > requiring a diagnostic. Everything after that is outside the scope of
    > the Standard (both C89 and C99, I believe).
    >
    >> //#pragma pack()

    >
    >> int main(int arg)
    >> {
    >> printf("size of tNullMsg is %d\n", sizeof(tNullMsg));
    >> printf("size of tCharMsg is %d\n", sizeof(tCharMsg));
    >> }

    >
    >> who can explain it ?
    >> thanks very much!

    >
    > People who understand the use of your implementation - seek another
    > newsgroup for that.


    And "#pragma pack" is non-standard. It should be explained in your
    implementation's documentation.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 16, 2005
    #3
  4. Lokicer

    pemo Guest

    If you're using VC, you should see for errors/warnings about this - like
    C2229, C2123 - it should error if you're using the /Za switch I think?

    The only time I've seen something like this before is, for example, here:

    typedef struct _RECOGNIZEDATATABLE
    {
    WORD nRecognizeDatas; //... number of RECOGNIZEDATA
    structures
    RECOGNIZEDATA RecognizeData[0]; //... array of RECOGNIZEDATA
    structures follows
    } RECOGNIZEDATATABLE;

    typedef struct _RECOGNIZEDATA
    {
    WORD ProtocolID; //... Protocol which was recognized
    WORD nProtocolOffset; //... Offset from the start of the frame
    of the start of this protocol.
    LPVOID InstData; //... Opaque, for protocol only.
    } RECOGNIZEDATA;

    The use if fairly obvious I think?

    Interested in comments about how to do this using -ansi though.


    "Christopher Benson-Manica" <> wrote in message
    news:ditm5q$c0l$...
    > Lokicer <> wrote:
    >
    >> typedef struct tag_NullMsg
    >> {
    >> int a[0];
    >> } tNullMsg;

    >
    > Attempting to use a zero-length array is a constraint violation
    > requiring a diagnostic. Everything after that is outside the scope of
    > the Standard (both C89 and C99, I believe).
    >
    >> //#pragma pack()

    >
    >> int main(int arg)
    >> {
    >> printf("size of tNullMsg is %d\n", sizeof(tNullMsg));
    >> printf("size of tCharMsg is %d\n", sizeof(tCharMsg));
    >> }

    >
    >> who can explain it ?
    >> thanks very much!

    >
    > People who understand the use of your implementation - seek another
    > newsgroup for that.
    >
    > http://www.ungerhu.com/jxh/clc.welcome.txt
    > http://www.eskimo.com/~scs/C-faq/top.html
    > http://benpfaff.org/writings/clc/off-topic.html
    >
    > --
    > Christopher Benson-Manica | I *should* know what I'm talking about - if I
    > ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    pemo, Oct 16, 2005
    #4
  5. Lokicer

    Harry Plaske Guest

    Your VC 6.0 Compiler should at least tell you the Warning C4200
    (warning level 4) -> Null-Feld in Struktur/Union

    moreover changing the tags inside the struct results to the compiler
    error C2229

    typedef struct tag_CharMsg
    {
    int a[0];
    char c;
    } tCharMsg;

    writing portable C programs should exclude the use of #pragma pack

    In your example the comiler tries to eliminate the natural alignment
    thus allocating every tag symbol to sucessive adresses

    more information is in the chapter "pack" of your online reference

    be careful: your alignment depends from the default value which can
    be configured .


    apart you shoult insert a return 0 statement at the end of main


    On Sun, 16 Oct 2005 15:57:15 +0800, "Lokicer" <> wrote:

    >Hi, i am a c newbie, i write some code to get size of structs
    >i compile and run it in VC6.0
    >
    >//#pragma pack(1)
    >
    >typedef struct tag_NullMsg
    >{
    > int a[0];
    >} tNullMsg;
    >typedef struct tag_CharMsg
    >{
    > char c;
    > int a[0];
    >} tCharMsg;
    >
    >//#pragma pack()
    >
    >int main(int arg)
    >{
    > printf("size of tNullMsg is %d\n", sizeof(tNullMsg));
    > printf("size of tCharMsg is %d\n", sizeof(tCharMsg));
    >}
    >
    >when i open #pragma pack(1) switch the size of tNullMsg is 1 and tCharMsg is
    >1

    No, on my maschine this results to 4
    >when i close #pragma pack(1) switch the size of tNullMsg is 1 and tCharMsg
    >is 4
    >
    >who can explain it ?
    >thanks very much!
    >
    >Lokicer
    >
     
    Harry Plaske, Oct 16, 2005
    #5
    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. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    670
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    590
    Chris Hauxwell
    Apr 27, 2004
  3. luke

    Size of structs containing unions

    luke, Sep 30, 2005, in forum: C Programming
    Replies:
    13
    Views:
    613
  4. Paminu
    Replies:
    5
    Views:
    661
    Eric Sosman
    Oct 11, 2005
  5. Daniel Rudy
    Replies:
    15
    Views:
    1,452
    Keith Thompson
    Apr 10, 2006
Loading...

Share This Page