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

L

Lokicer

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
 
C

Christopher Benson-Manica

Lokicer said:
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
 
K

Keith Thompson

Christopher Benson-Manica said:
Lokicer said:
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.
 
P

pemo

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 said:
Lokicer said:
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
 
H

Harry Plaske

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


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top