C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Sizeof empty class
Goto page 1, 2, 3, 4, 5  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Rohit Dhamija
Guest





PostPosted: Wed Jul 28, 2004 1:59 am    Post subject: Sizeof empty class Reply with quote



Dear All,

I have a small doubt, what is the size of an empty class (having no
member variable/functions defined) and why?

I tried and ran following program in vc++ version 6.0 and got the
answer 1
What does this 1 represent. Is 1 means that the size is 1 byte. But
what is stored in this memory location ??? A pointer to the the class
?

#include <iostream.h>
class base {
};
void main()
{
cout< }

Please comment!!

Regards,
Rohit

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Paul D. DeRocco
Guest





PostPosted: Wed Jul 28, 2004 8:08 am    Post subject: Re: Sizeof empty class Reply with quote



Quote:
"Rohit Dhamija" <rohit_dhamija (AT) rediffmail (DOT) com> wrote

I have a small doubt, what is the size of an empty class (having no
member variable/functions defined) and why?

I tried and ran following program in vc++ version 6.0 and got the
answer 1
What does this 1 represent. Is 1 means that the size is 1 byte. But
what is stored in this memory location ??? A pointer to the the class
?

#include class base {
};
void main()
{
cout< }

Please comment!!

1 is correct. Everything has to have a sizeof at least 1, so that the
address of each element of an array is unique.

--

Ciao, Paul D. DeRocco
Paul mailto:pderocco (AT) ix (DOT) netcom.com


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dave Townsend
Guest





PostPosted: Wed Jul 28, 2004 8:12 am    Post subject: Re: Sizeof empty class Reply with quote




I believe the size is set to 1 as this is the smallest non-zero size
you can have. I reasoned that a non-zero size would be necessary
to give correct meaning to things like foo f[10], if foo had zero size,
then indexing into the array would always access the same element, thus
it would not be possible to distinguish elements contained at f[0] and f[1]

If you want to try this experiment, go ahead, create an empty class and
then construct an array of 10 elements, then index through the class and
see that the "this" pointer is actually different for each of the elements.




"Rohit Dhamija" <rohit_dhamija (AT) rediffmail (DOT) com> wrote

Quote:
Dear All,

I have a small doubt, what is the size of an empty class (having no
member variable/functions defined) and why?

I tried and ran following program in vc++ version 6.0 and got the
answer 1
What does this 1 represent. Is 1 means that the size is 1 byte. But
what is stored in this memory location ??? A pointer to the the class
?

#include class base {
};
void main()
{
cout< }

Please comment!!


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ulrich Eckhardt
Guest





PostPosted: Wed Jul 28, 2004 8:14 am    Post subject: Re: Sizeof empty class Reply with quote

Rohit Dhamija wrote:
Quote:
I have a small doubt, what is the size of an empty class (having no
member variable/functions defined) and why?

Every object has at least size one, because that is required for it to have
a unique address. Note: functions don't count in that calculation.

Quote:
I tried and ran following program in vc++ version 6.0 and got the
answer 1 What does this 1 represent. Is 1 means that the size is 1
byte.

'1' means that it has the same size as a 'char', on your platform that is
the size of a byte, but it doesn't have to.

Quote:
But what is stored in this memory location ??? A pointer to
the the class?

Nothing.

In case you are interested, you might try to see how deriving from one or
more empty classes changes the size of the final object. Search the web
for 'empty baseclass optimization'.

Quote:
#include

Ewww, what book did you get that from?

Uli

--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Wed Jul 28, 2004 10:01 pm    Post subject: Re: Sizeof empty class Reply with quote

Ulrich Eckhardt wrote:
Quote:
Rohit Dhamija wrote:
I have a small doubt, what is the size of an empty class (having no
member variable/functions defined) and why?

Every object has at least size one, because that is required for it to have
a unique address. Note: functions don't count in that calculation.

To be precise, this is true of every *complete* object. As you know,
in an instance of a class derived from a base class with no non-static
member data, the size of the sub-object of the base class type can be
zero.

Quote:
I tried and ran following program in vc++ version 6.0 and got the
answer 1 What does this 1 represent. Is 1 means that the size is 1
byte.

'1' means that it has the same size as a 'char', on your platform that is
the size of a byte, but it doesn't have to.
snip


Yes it does (paragraph 5.3.3/1).

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ulrich Eckhardt
Guest





PostPosted: Thu Jul 29, 2004 3:22 pm    Post subject: Re: Sizeof empty class Reply with quote

Ben Hutchings wrote:
Quote:
Ulrich Eckhardt wrote:
Every object has at least size one, because that is required for it to
have a unique address.

To be precise, this is true of every *complete* object.

Hmmm, I don't think I understand you. I know what an incomplete type is, but
what is an incomplete object? An object whos ctor hasn't finished yet?

Quote:
As you know, in an instance of a class derived from a base class with no
non-static member data, the size of the sub-object of the base class type
can be zero.

Just for confirmation: isn't that what 'empty baseclass optimization' is
about? And is there a case where sizeof yields zero?

As I understand it, the size required for an object can be zero (when it is
a sub-object and EBO kicks in) but the result of sizeof can never be zero.

Quote:
I tried and ran following program in vc++ version 6.0 and got the
answer 1 What does this 1 represent. Is 1 means that the size is 1
byte.

'1' means that it has the same size as a 'char', on your platform that is
the size of a byte, but it doesn't have to.
snip

Yes it does (paragraph 5.3.3/1).

Argh. Yes, The Standard even defines what a byte is, so I should have said
'octet' instead of byte.

Uli

--
FAQ: http://parashift.com/c++-faq-lite/

/* bittersweet C++ */
default: break;

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tobias Güntner
Guest





PostPosted: Thu Jul 29, 2004 9:11 pm    Post subject: Re: Sizeof empty class Reply with quote

Ulrich Eckhardt wrote:

Quote:
Ben Hutchings wrote:

Ulrich Eckhardt wrote:

Every object has at least size one, because that is required for
it to have a unique address.

To be precise, this is true of every *complete* object.


Hmmm, I don't think I understand you. I know what an incomplete type
is, but what is an incomplete object? An object whos ctor hasn't
finished yet?


AFAIK a complete object is the enclosing object, e.g.

class B {};
class C {};
class A : public B
{
C c;
};

While B and c are perfectly valid objects, they're only sub-objects of
A, i.e. A is the complete object. The required memory for B may be
optimized away, but the required memory for c may not.


Quote:

As you know, in an instance of a class derived from a base class
with no non-static member data, the size of the sub-object of the
base class type can be zero.


Just for confirmation: isn't that what 'empty baseclass optimization'
is about?
yes


Quote:
And is there a case where sizeof yields zero?
no


Quote:
As I understand it, the size required for an object can be zero (when
it is a sub-object and EBO kicks in) but the result of sizeof can
never be zero.


yes.


In my example above, sizeof(B) is always >=1 but (figuratively speaking)
sizeof(A::B) can be zero.

Note that in this example:

class D : public B {};
class E : public B {};
class F : public D, public E {};

both B sub-objects must have a different memory address, therefore
sizeof(F) is at least 2, although the class is basically empty and
neither B requires memory.


--
Regards,
Tobias

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Thu Jul 29, 2004 9:14 pm    Post subject: Re: Sizeof empty class Reply with quote

Ulrich Eckhardt wrote:
Quote:
Ben Hutchings wrote:
Ulrich Eckhardt wrote:
Every object has at least size one, because that is required for it to
have a unique address.

To be precise, this is true of every *complete* object.

Hmmm, I don't think I understand you. I know what an incomplete type is, but
what is an incomplete object? An object whos ctor hasn't finished yet?

An "incomplete" object is a sub-object. Actually what you said is
true of two kinds of sub-object: members of a class-type object and
elements of an array.

Quote:
As you know, in an instance of a class derived from a base class with no
non-static member data, the size of the sub-object of the base class type
can be zero.

Just for confirmation: isn't that what 'empty baseclass optimization' is
about?

Right.

Quote:
And is there a case where sizeof yields zero?

As I understand it, the size required for an object can be zero (when it is
a sub-object and EBO kicks in) but the result of sizeof can never be zero.
snip


Right - it always yields the size of a complete object of the given
type or of the same type as the given expression.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Falk Tannhäuser
Guest





PostPosted: Fri Jul 30, 2004 9:27 am    Post subject: Re: Sizeof empty class Reply with quote

Ulrich Eckhardt wrote:

Quote:
Ben Hutchings wrote:

Ulrich Eckhardt wrote:

Every object has at least size one, because that is required for it to
have a unique address.

To be precise, this is true of every *complete* object.


Hmmm, I don't think I understand you. I know what an incomplete type is, but
what is an incomplete object? An object whos ctor hasn't finished yet?

A base class subobject within a derived class object, I suppose.
BTW, how does one call objects of incomplete type, like

extern char const msg[];

or

class Foo;
extern Foo f;

--
Falk


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Chris Uzdavinis
Guest





PostPosted: Fri Jul 30, 2004 9:28 am    Post subject: Re: Sizeof empty class Reply with quote

Ben Hutchings <do-not-spam-benh (AT) bwsint (DOT) com> wrote


Quote:
'1' means that it has the same size as a 'char', on your platform that is
the size of a byte, but it doesn't have to.
snip

Yes it does (paragraph 5.3.3/1).

The "it" he was referring to was the size of an empty class, not the
size of a char. A char must be 1 byte, as you have shown, but the
size of an empty class can be >= 1 byte It's usually only 1 byte
since the space isn't used except for its unique address, but that's
up to the compiler vendor.

--
Chris

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Allan W
Guest





PostPosted: Mon Aug 02, 2004 10:51 pm    Post subject: Re: Sizeof empty class Reply with quote

Tobias Güntner <fatbull (AT) users (DOT) sourceforge.net> wrote
Quote:
class B {};
class C {};
class A : public B
{
C c;
};
While B and c are perfectly valid objects, they're only sub-objects of
A, i.e. A is the complete object. The required memory for B may be
optimized away, but the required memory for c may not.

Okay.

Quote:
In my example above, sizeof(B) is always >=1 but (figuratively speaking)
sizeof(A::B) can be zero.

I'm not sure what "figuratively speaking" was supposed to imply, but
surely sizeof(A::B) is the same as sizeof(B)?

Quote:
Note that in this example:

class D : public B {};
class E : public B {};
class F : public D, public E {};

both B sub-objects must have a different memory address, therefore
sizeof(F) is at least 2, although the class is basically empty and
neither B requires memory.

Can you tell me where in the standard it says that the B sub-objects
have to have different addresses?

Microsoft Visual C 6.0 gives sizeof(F)==1, and I don't think this is
wrong. Do you know of some system where sizeof(D)==sizeof(E)==1, but
sizeof(F)==2?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Hyman Rosen
Guest





PostPosted: Wed Aug 04, 2004 9:59 am    Post subject: Re: Sizeof empty class Reply with quote

Allan W wrote:
Quote:
I'm not sure what "figuratively speaking" was supposed to imply, but
surely sizeof(A::B) is the same as sizeof(B)?

He meant the actual number of bytes dedicated to A::B within the larger
object, as opposed to the number returned by sizeof (which is the size
of the type).

Quote:
Can you tell me where in the standard it says that the B sub-objects
have to have different addresses?

10/5

Quote:
Microsoft Visual C 6.0 gives sizeof(F)==1

This is perfectly plausible; the other poster was wrong, because the
layout can be
D::B, D, pad, E::B, E, F

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Bill Wade
Guest





PostPosted: Wed Aug 04, 2004 10:13 am    Post subject: Re: Sizeof empty class Reply with quote

Quote:
Can you tell me where in the standard it says that the B sub-objects
have to have different addresses?

10p5 "Note: ... two [base class] subobjects that have the same class
type and that belong to the same most derived object must not be
allocated at the same address."

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tobias Güntner
Guest





PostPosted: Wed Aug 04, 2004 12:07 pm    Post subject: Re: Sizeof empty class Reply with quote

Allan W wrote:
Quote:
In my example above, sizeof(B) is always >=1 but (figuratively speaking)
sizeof(A::B) can be zero.


I'm not sure what "figuratively speaking" was supposed to imply, but
surely sizeof(A::B) is the same as sizeof(B)?


Yes, sizeof(A::B) is equal to sizeof(B). I wanted to say
sizeof(Base class of A which is A::B)==0
(I don't know how to express that in C++)

Quote:
Note that in this example:

class D : public B {};
class E : public B {};
class F : public D, public E {};

both B sub-objects must have a different memory address, therefore
sizeof(F) is at least 2, although the class is basically empty and
neither B requires memory.

Can you tell me where in the standard it says that the B sub-objects
have to have different addresses?


I don't know exactly where it is, but AFAIU two objects (any object, not
only sub-objects) of the same type (class B in our case) must never
share the same memory address (otherwise it would be impossible to
distinguish between them)

Quote:
Microsoft Visual C 6.0 gives sizeof(F)==1, and I don't think this is
wrong. Do you know of some system where sizeof(D)==sizeof(E)==1, but
sizeof(F)==2?


I've compiled it in VC6 as well and I also got sizeof(F)==1.
Then I've compiled the same test program in g++ and g++ prints
sizeof(F)==2. I believe VC6 is wrong.



--
Regards,
Tobias

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Thu Aug 05, 2004 3:17 am    Post subject: Re: Sizeof empty class Reply with quote

In article <ceokm9$84t$07$1 (AT) news (DOT) t-online.com>, Tobias Güntner
<fatbull (AT) users (DOT) sourceforge.net> writes
Quote:
I've compiled it in VC6 as well and I also got sizeof(F)==1.
Then I've compiled the same test program in g++ and g++ prints
sizeof(F)==2. I believe VC6 is wrong.

But to demonstrate that you must show that the two instances of class B

share the same address. Note that C++ in fact only requires distinct
addresses and does not permit you to write to those addresses.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2, 3, 4, 5  Next
Page 1 of 5

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.