473,401 Members | 2,146 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,401 software developers and data experts.

size of a struct

Consider the following program fragment:

/************************/
struct some_struct {

}a;

printf("%d", sizeof(a));
/*************************/

On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don't know the compiler :-( } the output
is 1(one).

Is it compiler dependent?
If so how can a variable like "a" have a zero size ( I mean how can a
variable with 0(zero) byte size be stored in memory).

Sep 5 '06 #1
15 2834
chandanlinster wrote:
Consider the following program fragment:

/************************/
struct some_struct {

}a;

printf("%d", sizeof(a));
/*************************/

On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don't know the compiler :-( } the output
is 1(one).

Is it compiler dependent?
If so how can a variable like "a" have a zero size ( I mean how can a
variable with 0(zero) byte size be stored in memory).
First of all sizeof returns unsigned int so it should be
printf("%u", sizeof(a));
What you wrote evokes undefined behaviour.

Second you are declaring a structure with no members
which means you cannot assign anything to it. So a zero
size doesn't look unreasonable.

Third , I don't know if structures with no members are
allowed by the standard. The GNU compiler gives a
warning while the SUN compiler and lint give syntax
error. All this happens for the following code:

#include <stdio.h>

int main(void) {
struct some_struct { }a;

printf("%u", sizeof(a));
}

Sep 5 '06 #2
"chandanlinster" <ch************@gmail.comwrites:
Consider the following program fragment:

/************************/
struct some_struct {

}a;

printf("%d", sizeof(a));
/*************************/
Standard C doesn't allow structures with no members.

If gcc allows this, it's a compiler-specific extension. If you're
curious about how it works, check the gcc documentation; if that
fails, try the gnu.gcc.help newsgroup.

--
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.
Sep 5 '06 #3
Spiros Bousbouras wrote:
chandanlinster wrote:
>Consider the following program fragment:

/************************/
struct some_struct {

}a;

printf("%d", sizeof(a));
/*************************/

On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don't know the compiler :-( } the output
is 1(one).

Is it compiler dependent?
If so how can a variable like "a" have a zero size ( I mean how can a
variable with 0(zero) byte size be stored in memory).

First of all sizeof returns unsigned int so it should be
printf("%u", sizeof(a));
What you wrote evokes undefined behaviour.
It yields a size_t, which might or might not be
an unsigned int.
printf("%lu", (unsigned long)sizeof(a)); is usually recommended for
c89
Sep 5 '06 #4
"chandanlinster" <ch************@gmail.comwrote:
Consider the following program fragment:

/************************/
struct some_struct {

}a;

printf("%d", sizeof(a));
/*************************/

On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don't know the compiler :-( } the output
is 1(one).
Neither is correct. An empty struct declaration is syntactically
invalid. Even one which only has unnamed members (which would at least
have a size) invokes undefined behaviour.
Is it compiler dependent?
It shouldn't be; it should be refused outright, rather than give a
random answer.
If so how can a variable like "a" have a zero size
It can't. That is presumably one of the reasons why it's disallowed.

Richard
Sep 5 '06 #5
Nils O. Selåsdal wrote:
Spiros Bousbouras wrote:
chandanlinster wrote:
Consider the following program fragment:

/************************/
struct some_struct {

}a;

printf("%d", sizeof(a));
/*************************/

On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don't know the compiler :-( } the output
is 1(one).

Is it compiler dependent?
If so how can a variable like "a" have a zero size ( I mean how can a
variable with 0(zero) byte size be stored in memory).
First of all sizeof returns unsigned int so it should be
printf("%u", sizeof(a));
What you wrote evokes undefined behaviour.
It yields a size_t, which might or might not be
an unsigned int.
printf("%lu", (unsigned long)sizeof(a)); is usually recommended for
c89
Quote from N1124 , section 6.5.3.4 , paragraph 4:

The value of the result is implementation-defined
and its type (an unsigned integer type) is size_t,
defined in <stddef.h(and other headers).

Sep 5 '06 #6
Spiros Bousbouras wrote:
Nils O. Selåsdal wrote:
>Spiros Bousbouras wrote:
>>chandanlinster wrote:

Consider the following program fragment:

/************************/
struct some_struct {

}a;

printf("%d", sizeof(a));
/*************************/

On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don't know the compiler :-( } the output
is 1(one).

Is it compiler dependent?
If so how can a variable like "a" have a zero size ( I mean how can a
variable with 0(zero) byte size be stored in memory).
First of all sizeof returns unsigned int so it should be
printf("%u", sizeof(a));
What you wrote evokes undefined behaviour.
It yields a size_t, which might or might not be
an unsigned int.
printf("%lu", (unsigned long)sizeof(a)); is usually recommended for
c89

Quote from N1124 , section 6.5.3.4 , paragraph 4:

The value of the result is implementation-defined
and its type (an unsigned integer type) is size_t,
defined in <stddef.h(and other headers).
Correct.
"an unsigned integer" might not be an unsigned int.
Thus we cast it to an unsigned long which is the largest
unsigned integer C90 guarantees, and print it accordingly.
Sep 5 '06 #7
Nils O. Selåsdal wrote:
Spiros Bousbouras wrote:
Nils O. Selåsdal wrote:
Spiros Bousbouras wrote:
chandanlinster wrote:

Consider the following program fragment:

/************************/
struct some_struct {

}a;

printf("%d", sizeof(a));
/*************************/

On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don't know the compiler :-( } the output
is 1(one).

Is it compiler dependent?
If so how can a variable like "a" have a zero size ( I mean how can a
variable with 0(zero) byte size be stored in memory).
First of all sizeof returns unsigned int so it should be
printf("%u", sizeof(a));
What you wrote evokes undefined behaviour.
It yields a size_t, which might or might not be
an unsigned int.
printf("%lu", (unsigned long)sizeof(a)); is usually recommended for
c89
Quote from N1124 , section 6.5.3.4 , paragraph 4:

The value of the result is implementation-defined
and its type (an unsigned integer type) is size_t,
defined in <stddef.h(and other headers).
Correct.
"an unsigned integer" might not be an unsigned int.
Thus we cast it to an unsigned long which is the largest
unsigned integer C90 guarantees, and print it accordingly.
So in C99 we would have to cast it to unsigned long long ?

Sep 5 '06 #8
Spiros Bousbouras wrote:
Nils O. Selåsdal wrote:
>Spiros Bousbouras wrote:
>>Nils O. Selåsdal wrote:

Spiros Bousbouras wrote:
chandanlinster wrote:
>
>Consider the following program fragment:
>>
>/************************/
>struct some_struct {
>>
>}a;
>>
>printf("%d", sizeof(a));
>/*************************/
>>
>On GCC 4.1.1 the output is 0(zero).
>On another compiler {sorry I don't know the compiler :-( } the output
>is 1(one).
>>
>Is it compiler dependent?
>If so how can a variable like "a" have a zero size ( I mean how can a
>variable with 0(zero) byte size be stored in memory).
First of all sizeof returns unsigned int so it should be
printf("%u", sizeof(a));
What you wrote evokes undefined behaviour.
It yields a size_t, which might or might not be
an unsigned int.
printf("%lu", (unsigned long)sizeof(a)); is usually recommended for
c89
Quote from N1124 , section 6.5.3.4 , paragraph 4:

The value of the result is implementation-defined
and its type (an unsigned integer type) is size_t,
defined in <stddef.h(and other headers).
Correct.
"an unsigned integer" might not be an unsigned int.
Thus we cast it to an unsigned long which is the largest
unsigned integer C90 guarantees, and print it accordingly.

So in C99 we would have to cast it to unsigned long long ?
C99 provides the 'z' length modifier for the size_t type, so there we
might just do printf("%zu",sizeof a);
Sep 5 '06 #9
"Spiros Bousbouras" <sp****@gmail.comwrites:
Nils O. Selåsdal wrote:
>Spiros Bousbouras wrote:
Nils O. Selåsdal wrote:

Spiros Bousbouras wrote:
[...]
>>First of all sizeof returns unsigned int so it should be
printf("%u", sizeof(a));
What you wrote evokes undefined behaviour.
It yields a size_t, which might or might not be
an unsigned int.
printf("%lu", (unsigned long)sizeof(a)); is usually recommended for
c89

Quote from N1124 , section 6.5.3.4 , paragraph 4:

The value of the result is implementation-defined
and its type (an unsigned integer type) is size_t,
defined in <stddef.h(and other headers).
Correct.
"an unsigned integer" might not be an unsigned int.
Thus we cast it to an unsigned long which is the largest
unsigned integer C90 guarantees, and print it accordingly.

So in C99 we would have to cast it to unsigned long long ?
If your runtime library is C99 compliant (which it might not be even
if the compiler is), you can use "%zu".

Or you can just cast to unsigned long and use "%lu". Even if size_t
is bigger than unsigned long, still work as long as the value you're
printing doesn't exceed ULONG_MAX.

--
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.
Sep 5 '06 #10
In article <11**********************@b28g2000cwb.googlegroups .com>
chandanlinster <ch************@gmail.comwrote:
>Consider the following program fragment:
struct some_struct { } a;
printf("%d", sizeof(a));
On GCC 4.1.1 the output is 0(zero).
I do not actually have gcc 4.1.1 handy, but I do not believe this
is the case. Note that to get gcc to compile C code (as opposed to
some other language that looks a lot like C, but is not actually C)
you must give it the "-ansi -pedantic" options, and if you do so:

% strictcc t.c
t.c:1: error: struct has no members

(where "strictcc" is "cc -ansi -pedantic -Werror" with the stderr
stream massaged to change the string "warning" to "error":

% cat ~/scripts/strictcc
#! /bin/sh
(cc -ansi -pedantic -Werror ${1+"$@"} 2>&1) |
sed -e 1d -e s/warning:/error:/
# it might be reasonable to add 1>&2 but this suffices for now

If gcc ever claims full C99 conformance, you can use -std=c99
instead of -ansi or -std=c89, presumably still with -pedantic,
to get a strict C99 compiler.)
>On another compiler {sorry I don't know the compiler :-( } the output
is 1(one).
This would be correct for a C++ compiler -- are you sure you invoked
a C compiler? (Note that, as with gcc, you may have to supply
various command-line arguments to get compiler for the C language,
rather than one for some other language that looks a lot like C,
but is different.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Sep 5 '06 #11
chandanlinster wrote:
Consider the following program fragment:

/************************/
struct some_struct {

}a;

printf("%d", sizeof(a));
/*************************/

On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don't know the compiler :-( } the output
is 1(one).

Is it compiler dependent?
If so how can a variable like "a" have a zero size ( I mean how can a
variable with 0(zero) byte size be stored in memory).
In your view, what should be the size of a structure with no members?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Sep 6 '06 #12
In article <ed*********@news4.newsguy.comChris Torek <no****@torek.netwrites:
In article <11**********************@b28g2000cwb.googlegroups .com>
chandanlinster <ch************@gmail.comwrote:
Consider the following program fragment:
struct some_struct { } a;
printf("%d", sizeof(a));
On GCC 4.1.1 the output is 0(zero).

I do not actually have gcc 4.1.1 handy, but I do not believe this
is the case.
4.0.2 also does it. With -ansi (or other flags) it does indeed also give
the warning.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Sep 6 '06 #13
Richard Bos wrote:
"chandanlinster" <ch************@gmail.comwrote:
Consider the following program fragment:

/************************/
struct some_struct {

}a;

printf("%d", sizeof(a));
/*************************/

On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don't know the compiler :-( } the output
is 1(one).

Neither is correct. An empty struct declaration is syntactically
invalid. Even one which only has unnamed members (which would at least
have a size) invokes undefined behaviour.
Is it compiler dependent?

It shouldn't be;
Like all undefined behavior, it is of course compiler-dependent.
it should be refused outright, rather than give a
random answer.
Refused outright is a bit strong. There is no requirement that the
compiler reject anything except for an #error directive that survives
preprocessing. There is nothing wrong with GCC allowing it as an extension.
If so how can a variable like "a" have a zero size

It can't. That is presumably one of the reasons why it's disallowed.

Richard

--
Clark S. Cox III
cl*******@gmail.com
Sep 6 '06 #14
chandanlinster wrote:
Consider the following program fragment:

/************************/
struct some_struct {

}a;

printf("%d", sizeof(a));
/*************************/

On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don't know the compiler :-( } the output
is 1(one).
Your other compiler very well may have been a C++ compiler (where empty
structs are defined, and produce a valid value when used with the sizeof
operator).
>
Is it compiler dependent?
Yes. It is undefined behavior, in C a struct must have at least one
named member; A C compiler is allowed to do anything or nothing at all.
To put it bluntly, don't do this :)
If so how can a variable like "a" have a zero size ( I mean how can a
variable with 0(zero) byte size be stored in memory).
It can't.
--
Clark S. Cox III
cl*******@gmail.com
Sep 6 '06 #15
On 5 Sep 2006 18:29:37 GMT, Chris Torek <no****@torek.netwrote:
% strictcc t.c
t.c:1: error: struct has no members

(where "strictcc" is "cc -ansi -pedantic -Werror" with the stderr
stream massaged to change the string "warning" to "error":

% cat ~/scripts/strictcc
#! /bin/sh
(cc -ansi -pedantic -Werror ${1+"$@"} 2>&1) |
sed -e 1d -e s/warning:/error:/
# it might be reasonable to add 1>&2 but this suffices for now
<IMPLSPECIFIC=GCC-pedantic-errors makes them errors (fail
compilation) (but without putting the string "error:"). This is
consistent with what -Werror does for other warnings. </>
- David.Thompson1 at worldnet.att.net
Sep 21 '06 #16

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

Similar topics

6
by: MSReddy | last post by:
Hi Group, I have a structure than has other structures in it. I added 3 members to a strucuture and my program started behaving very strangly. When I printed sizes of the strucutures the values...
7
by: ANaiveProgrammer | last post by:
Hi all I have made the following two structs and size is not according to what is supposed to be, so please ponder over following and identify if im wrong... please also mention what would be...
19
by: junky_fellow | last post by:
Can the size of pointer variables of different type may be different on a particular architecture. For eg. Can the sizeof (char *) be different from sizeof(int *) or sizeof (void *) ? What...
3
by: Dirk Reske | last post by:
Hello, I have the following struct: public struct WAVEFORMATEX { public UInt16 wFormatTag; //2 bytes public UInt16 nChannels; //2 bytes public UInt32 nSamplesPerSec; //4 bytes...
1
by: Jón Sveinsson | last post by:
Hello everyone I have been trying to read and write struct to binary files, I'm using to functions to convert the struct to bytes and bytes to struct, I always receive the following error ...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
11
by: chinu | last post by:
mail hi all.. is it possible to find the size of a structure upto bit size.., where the structure contains only one element and that too with bit specifier..eg.. struct x { char a : 4; } y;
111
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- ...
6
by: marktxx | last post by:
Although the C90 standard only mentions the use of 'signed int' and 'unsigned int' for bit-fields (use 'int' at your own risk) and C99 adds _Bool. It seems that most compilers create the size of...
15
by: kris | last post by:
Hi I am writing a small program where I need to obtain the actual size of a structure. The programm is as follows struct abc { int j; char k; int i; }*a;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.