size of a struct

C

chandanlinster

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).
 
S

Spiros Bousbouras

chandanlinster said:
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));
}
 
K

Keith Thompson

chandanlinster said:
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.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Spiros said:
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
 
R

Richard Bos

chandanlinster said:
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
 
S

Spiros Bousbouras

Nils said:
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).
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Spiros said:
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.
 
S

Spiros Bousbouras

Nils said:
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 ?
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Spiros said:
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);
 
K

Keith Thompson

Spiros Bousbouras said:
Nils said:
Spiros said:
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.
 
C

Chris Torek

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.)
 
J

Joe Wright

chandanlinster said:
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?
 
D

Dik T. Winter

>
> 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.
 
C

Clark S. Cox III

Richard said:
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.


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.
 
C

Clark S. Cox III

chandanlinster said:
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.
 
D

Dave Thompson

% 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
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top