size of a struct

Discussion in 'C Programming' started by chandanlinster, Sep 5, 2006.

  1. 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).
     
    chandanlinster, Sep 5, 2006
    #1
    1. Advertising

  2. 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));
    }
     
    Spiros Bousbouras, Sep 5, 2006
    #2
    1. Advertising

  3. "chandanlinster" <> writes:
    > 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) <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.
     
    Keith Thompson, Sep 5, 2006
    #3
  4. 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
     
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Sep 5, 2006
    #4
  5. chandanlinster

    Richard Bos Guest

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


    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
     
    Richard Bos, Sep 5, 2006
    #5
  6. 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).
     
    Spiros Bousbouras, Sep 5, 2006
    #6
  7. 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.
     
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Sep 5, 2006
    #7
  8. 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 ?
     
    Spiros Bousbouras, Sep 5, 2006
    #8
  9. 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);
     
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Sep 5, 2006
    #9
  10. "Spiros Bousbouras" <> writes:
    > 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) <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.
     
    Keith Thompson, Sep 5, 2006
    #10
  11. chandanlinster

    Chris Torek Guest

    In article <>
    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).


    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.
     
    Chris Torek, Sep 5, 2006
    #11
  12. chandanlinster

    Joe Wright Guest

    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 ---
     
    Joe Wright, Sep 6, 2006
    #12
  13. In article <> Chris Torek <> writes:
    > In article <>
    > 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).

    >
    > 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/
     
    Dik T. Winter, Sep 6, 2006
    #13
  14. Richard Bos 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).

    >
    > 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
     
    Clark S. Cox III, Sep 6, 2006
    #14
  15. 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
     
    Clark S. Cox III, Sep 6, 2006
    #15
  16. On 5 Sep 2006 18:29:37 GMT, Chris Torek <> 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
     
    Dave Thompson, Sep 21, 2006
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Fogelklou
    Replies:
    36
    Views:
    1,405
    Chris Fogelklou
    Apr 20, 2004
  2. Replies:
    5
    Views:
    558
    Flash Gordon
    Apr 9, 2006
  3. Replies:
    8
    Views:
    512
    Bob Hairgrove
    Apr 10, 2006
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,646
    Michael Jung
    May 25, 2008
  5. Jaco Naude
    Replies:
    2
    Views:
    1,484
    Jaco Naude
    Jan 13, 2010
Loading...

Share This Page