473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
Nov 15 '05 #1
4 2721
Lokicer <lo*****@163.com> 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.
Nov 15 '05 #2
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Lokicer <lo*****@163.com> 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) ks***@mib.org <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.
Nov 15 '05 #3
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" <at***@nospam.cyberspace.org> wrote in message
news:di**********@chessie.cirr.com...
Lokicer <lo*****@163.com> 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.

Nov 15 '05 #4


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" <lo*****@163.com> 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 4when i close #pragma pack(1) switch the size of tNullMsg is 1 and tCharMsg
is 4

who can explain it ?
thanks very much!

Lokicer


Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Joe | last post by:
I have an upload file operation in the web application. UploadForm.asp is the form, and UploadAction.asp is the form processing. //UploadForm.asp <FORM NAME="InputForm"...
4
by: JMUApache | last post by:
Hi, A have a problem with Inhreit Control. I try to Change its Size in Construct Function, And it works. Code Here: -------------------------------------------------- public class...
1
by: tim | last post by:
trying to work with a directory in windows, that has "\a" in the full pathname this works: >>> c = string.replace('c:\content\ce\cw\cvd', '\\', '\\') >>> c 'c:\\content\\ce\\cw\\cvd' this...
1
by: Paul Gorodyansky | last post by:
Hello! I have a drop-down list, 13 options there. If I click on it, Firefox shows all 13 items while Internet Explorer shows only 11 and a scroll bar - even if it's in the middle of a page and...
17
by: teser3 | last post by:
I can put out a Word Doc from PHP but if I try and manipulate the font size in the PHP part it outputs a blank Word Document. Here is what I have: <?php $fname="report.doc"; $handle = fopen(...
6
by: Warly girl | last post by:
Hi i want your help again in c++ !! i want a function change the "int" to "string" because in my project "Registration system" in task "b" i must write a course class which contains number of...
3
by: Tom | last post by:
Can someone please explain the non-working aspect of binding to Width? See code below. Thanks !! -- Tom =================================================== using System; using...
1
by: chrisj | last post by:
I'm using freeASPupload and got some assistance integrating to a Member script. It works successfully. In this modified version there are two groups that use this upload script. Members of one...
7
by: Luna Moon | last post by:
#include "stdafx.h" #include <iostream> #include <string> using namespace std; int main() { string cc(31, 'c'); string bb=cc.assign(3, 'dd');
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.