sizeof(EmptyStruct) in C and C++ (was: Base {}; sizeof(Base) == 1?)

Discussion in 'C Programming' started by Alex Vinokur, Aug 14, 2006.

  1. Alex Vinokur

    Alex Vinokur Guest

    "Alf P. Steinbach" <> wrote in message news:...
    > * :
    > > This may be stupid question, but why is sizeof(Base) == 1 in:
    > >
    > > int main(int argc, char* argv[])
    > > {
    > > class Base
    > > {
    > > };
    > > cout << sizeof(Base) << endl;
    > > return 0;
    > > }
    > >
    > > I guess I want to know what the 1 byte is for? There is no vptr here,
    > > so why 1 byte?I checked FAQ and couldn't find answer.

    >
    > Needs a unique address.
    >


    struct Empty {};

    C: sizeof(Empty) == 0
    C++: sizeof(Empty) > 0

    Why doesn't C need a unique address?


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Aug 14, 2006
    #1
    1. Advertising

  2. Re: sizeof(EmptyStruct) in C and C++ (was: Base {}; sizeof(Base) ==1?)

    "Alex Vinokur" <> writes:
    > "Alf P. Steinbach" <> wrote in message
    > news:...
    >> * :
    >> > This may be stupid question, but why is sizeof(Base) == 1 in:
    >> >
    >> > int main(int argc, char* argv[])
    >> > {
    >> > class Base
    >> > {
    >> > };
    >> > cout << sizeof(Base) << endl;
    >> > return 0;
    >> > }
    >> >
    >> > I guess I want to know what the 1 byte is for? There is no vptr here,
    >> > so why 1 byte?I checked FAQ and couldn't find answer.

    >>
    >> Needs a unique address.
    >>

    >
    > struct Empty {};
    >
    > C: sizeof(Empty) == 0
    > C++: sizeof(Empty) > 0
    >
    > Why doesn't C need a unique address?


    In C,
    struct Empty {};

    is a syntax error. (Some compilers might support that as an
    extension; if so, it's up to the compiler to decide what
    sizeof(struct Empty) should be.)

    --
    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, Aug 14, 2006
    #2
    1. Advertising

  3. Alex Vinokur

    swets Guest

    Keith Thompson wrote:
    > "Alex Vinokur" <> writes:
    > > "Alf P. Steinbach" <> wrote in message
    > > news:...
    > >> * :
    > >> > This may be stupid question, but why is sizeof(Base) == 1 in:
    > >> >
    > >> > int main(int argc, char* argv[])
    > >> > {
    > >> > class Base
    > >> > {
    > >> > };
    > >> > cout << sizeof(Base) << endl;
    > >> > return 0;
    > >> > }
    > >> >
    > >> > I guess I want to know what the 1 byte is for? There is no vptr here,
    > >> > so why 1 byte?I checked FAQ and couldn't find answer.
    > >>
    > >> Needs a unique address.
    > >>

    > >
    > > struct Empty {};
    > >
    > > C: sizeof(Empty) == 0
    > > C++: sizeof(Empty) > 0
    > >
    > > Why doesn't C need a unique address?

    >
    > In C,
    > struct Empty {};
    >
    > is a syntax error. (Some compilers might support that as an
    > extension; if so, it's up to the compiler to decide what
    > sizeof(struct Empty) should be.)
    >
    > --
    > 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.


    What is the size of a in
    int a[0];
    swets, Aug 14, 2006
    #3
  4. Re: sizeof(EmptyStruct) in C and C++

    swets wrote:

    > What is the size of a in
    > int a[0];


    A zero size array is illegal according to ANSI C.


    Igmar
    Igmar Palsenberg, Aug 14, 2006
    #4
  5. Alex Vinokur

    Sweta Guest

    Re: sizeof(EmptyStruct) in C and C++

    What if I use it in a structure like -
    struct A {
    int a[0];
    };

    What is the size of such structure?

    Thanks,
    Sweta

    Igmar Palsenberg wrote:
    > swets wrote:
    >
    > > What is the size of a in
    > > int a[0];

    >
    > A zero size array is illegal according to ANSI C.
    >
    >
    > Igmar
    Sweta, Aug 14, 2006
    #5
  6. Re: sizeof(EmptyStruct) in C and C++

    Sweta writes:
    > What if I use it in a structure like -
    > struct A {
    > int a[0];
    > };
    >
    > What is the size of such structure?


    Still not valid. Standard C does not allow 0-sized objects - and that
    includes objects that are part of other objects.

    --
    Hallvard
    Hallvard B Furuseth, Aug 14, 2006
    #6
  7. Re: sizeof(EmptyStruct) in C and C++

    Sweta wrote:
    > What if I use it in a structure like -
    > struct A {
    > int a[0];
    > };
    >
    > What is the size of such structure?


    What isn't clear about 'a zero size array is illegal' ? Putting it in a
    doesn't make it less of an array.


    Igmar
    Igmar Palsenberg, Aug 14, 2006
    #7
  8. Re: sizeof(EmptyStruct) in C and C++

    On 2006-08-14 04:55:31 -0400, "Sweta" <> said:
    >
    > Igmar Palsenberg wrote:
    >> swets wrote:
    >>
    >>> What is the size of a in
    >>> int a[0];

    >>
    >> A zero size array is illegal according to ANSI C.
    >>

    > What if I use it in a structure like -
    > struct A {
    > int a[0];
    > };
    >
    > What is the size of such structure?


    As Igmar said, "A zero size array is illegal according to ANSI C."

    --
    Clark S. Cox, III
    Clark S. Cox III, Aug 14, 2006
    #8
    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. Derek
    Replies:
    7
    Views:
    24,305
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    616
    CBFalconer
    Apr 10, 2004
  3. Replies:
    32
    Views:
    915
    Jerry Coffin
    Aug 18, 2006
  4. Alex Vinokur

    sizeof (size_t) and sizeof (pointer)

    Alex Vinokur, Nov 12, 2007, in forum: C++
    Replies:
    19
    Views:
    780
    Ben Rudiak-Gould
    Nov 30, 2007
  5. cppquester

    sizeof(derived) >= sizeof(base)?

    cppquester, Oct 7, 2011, in forum: C++
    Replies:
    1
    Views:
    303
    Juha Nieminen
    Oct 7, 2011
Loading...

Share This Page