Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   sizeof(EmptyStruct) in C and C++ (was: Base {}; sizeof(Base) == 1?) (http://www.velocityreviews.com/forums/t443972-sizeof-emptystruct-in-c-and-c-was-base-sizeof-base-1-a.html)

Alex Vinokur 08-14-2006 06:34 AM

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

"Alf P. Steinbach" <alfps@start.no> wrote in message news:4k9755Fb3nt6U1@individual.net...
> * moleskyca1@yahoo.com:
> > 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




Keith Thompson 08-14-2006 07:06 AM

Re: sizeof(EmptyStruct) in C and C++ (was: Base {}; sizeof(Base) ==1?)
 
"Alex Vinokur" <alexvn@users.sourceforge.net> writes:
> "Alf P. Steinbach" <alfps@start.no> wrote in message
> news:4k9755Fb3nt6U1@individual.net...
>> * moleskyca1@yahoo.com:
>> > 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) kst-u@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.

swets 08-14-2006 07:10 AM

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

Keith Thompson wrote:
> "Alex Vinokur" <alexvn@users.sourceforge.net> writes:
> > "Alf P. Steinbach" <alfps@start.no> wrote in message
> > news:4k9755Fb3nt6U1@individual.net...
> >> * moleskyca1@yahoo.com:
> >> > 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) kst-u@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.


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


Igmar Palsenberg 08-14-2006 07:50 AM

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


Sweta 08-14-2006 08:55 AM

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



Hallvard B Furuseth 08-14-2006 09:30 AM

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

Igmar Palsenberg 08-14-2006 09:32 AM

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

Clark S. Cox III 08-14-2006 04:57 PM

Re: sizeof(EmptyStruct) in C and C++
 
On 2006-08-14 04:55:31 -0400, "Sweta" <swetag@gmail.com> 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
clarkcox3@gmail.com



All times are GMT. The time now is 05:29 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57