freelanceprogrammers.org Forum Index » C

size of an empty class


View user's profile Post To page top
sudhir_kesha... Posted: Tue Jan 04, 2005 2:12 pm


Joined: 04 Jan 2005

Posts: 2
size of an empty class
hi all,
can anybody tell the reason
class myclass
{};
void main()
{
cout<<sizeof(myclass);
}

this program prints the size of class as 1 byte. can anybody tell me in details
why it keeps 1 bytes in memory., althouth the class is empty.

also do tell me what happens when we add member functions to this class.

Regards,
===========================
Sudhir Kesharwani
Mob : +91 9822511209
sudhir.kesharwani@...
===========================

--
_______________________________________________
Find what you are looking for with the Lycos Yellow Pages
http://r.lycos.com/r/yp_emailfooter/http://yellowpages.lycos.com/default.asp?SRC
=lycos10
Reply with quote
Send private message
View user's profile Post To page top
maniksinghal Posted: Wed Jan 05, 2005 5:11 pm


Joined: 02 Jan 2005

Posts: 9
size of an empty class
--- Sudhir Kesharwani <sudhir.kesharwani@...>
wrote:

>
> hi all,
> can anybody tell the reason
> class myclass
> {};
> void main()
> {
> cout<<sizeof(myclass);
> }
>
> this program prints the size of class as 1 byte.
> can anybody tell me in details why it keeps 1 bytes
> in memory., althouth the class is empty.
>
> also do tell me what happens when we add member
> functions to this class.
>
> Regards,
> ===========================
> Sudhir Kesharwani
> Mob : +91 9822511209
> sudhir.kesharwani@...
> ===========================

Hi sudhir,

The size of a class can never be 0 bytes. If it does
not contain any members, the compiler allocates 1 byte
for the class automatically as the class if defined is
a visible entity.
This does not mean that compiler always adds 1 byte to
the overall size of the class. It removes this byte as
soon as you add the first member to the class.

Regards
manik



>
> --
> _______________________________________________
> Find what you are looking for with the Lycos Yellow
> Pages
>
http://r.lycos.com/r/yp_emailfooter/http://yellowpages.lycos.com/default.asp?SRC
=lycos10
>
>
>
>
>





__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail
Reply with quote
Send private message
View user's profile Post To page top
shabble Posted: Wed Jan 05, 2005 5:29 pm


Joined: 29 Dec 2004

Posts: 31
size of an empty class
From: Sudhir Kesharwani [mailto:sudhir.kesharwani@...]

>hi all,
>can anybody tell the reason
>class myclass
>{};
>void main()
>{
>cout<<sizeof(myclass);
>}
>
>this program prints the size of class as 1 byte. can anybody
>tell me in details why it keeps 1 bytes in memory., althouth
>the class is empty.

It`s required by the standard. Two distinct objects *must* be distinct
(i.e. have different addresses), and the way to ensure this is to
require that classes must be at least one byte in size. (Think, e.g., of
an array of such objects)

>also do tell me what happens when we add member functions to
>this class.

The `phantom` byte disappears since there is now something in there that
takes up memory.

--
PJH

"Consistently separating words by spaces became a general custom about
the tenth century A.D., and lasted until about 1957, when FORTRAN
abandoned the practice." -- Sun FORTRAN Reference Manual



Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12
8JD, UK
Tel: +44(0)1454 294556 Fax: +44 (0)1454 299272

Website : www.alderley.com Sales : sales@... Service :
service@...

This email and its contents are confidential and are solely for the use of the
intended recipient. If you are not the original recipient you have received it
in error and any use, dissemination, forwarding, printing or copying of this
email is strictly prohibited. Should you receive this email in error please
immediately notify it@...

This email has been scanned for viruses, however you should always scan emails
with your own systems prior to opening.
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
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
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help