Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > size of a struct

Reply
Thread Tools

size of a struct

 
 
Chris Torek
Guest
Posts: n/a
 
      09-05-2006
In article <(E-Mail Removed) .com>
chandanlinster <(E-Mail Removed)> wrote:
>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 (4039.22'N, 11150.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.
 
Reply With Quote
 
 
 
 
Joe Wright
Guest
Posts: n/a
 
      09-06-2006
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 ---
 
Reply With Quote
 
 
 
 
Dik T. Winter
Guest
Posts: n/a
 
      09-06-2006
In article <(E-Mail Removed)> Chris Torek <(E-Mail Removed)> writes:
> In article <(E-Mail Removed) .com>
> chandanlinster <(E-Mail Removed)> wrote:
> >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/
 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      09-06-2006
Richard Bos wrote:
> "chandanlinster" <(E-Mail Removed)> 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).

>
> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      09-06-2006
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
(E-Mail Removed)
 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      09-21-2006
On 5 Sep 2006 18:29:37 GMT, Chris Torek <(E-Mail Removed)> wrote:

> % 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Struct size vs. Class size in memory Jaco Naude C++ 2 01-13-2010 10:59 AM
Could a struct with size 44 bytes point always points to a char array with size 2024 bytes? eagle_jyjh@citiz.net C++ 8 04-10-2006 03:05 PM
Could a struct with size 44 bytes point always points to a char array with size 2048 bytes? eagle_jyjh@citiz.net C Programming 5 04-09-2006 02:49 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments