![]() |
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 |
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. |
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]; |
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 |
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 |
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 |
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 |
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.