Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sizeof empty class

Reply
Thread Tools

sizeof empty class

 
 
deepakvsoni@gmail.com
Guest
Posts: n/a
 
      09-05-2007
what is the size of an class with not data members?
ex:
class A {
};

also
sizeof ?
class A{
int f(){
cout<<"Hello";
}
};

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      09-05-2007
<> wrote in message
news: oups.com...
> what is the size of an class with not data members?
> ex:
> class A {
> };
>
> also
> sizeof ?
> class A{
> int f(){
> cout<<"Hello";
> }
> };


Usually 1. Why don't you try it?


 
Reply With Quote
 
 
 
 
deepakvsoni@gmail.com
Guest
Posts: n/a
 
      09-05-2007
On Sep 5, 8:51 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> <deepakvs...@gmail.com> wrote in message
>
> news: oups.com...
>
> > what is the size of an class with not data members?
> > ex:
> > class A {
> > };

>
> > also
> > sizeof ?
> > class A{
> > int f(){
> > cout<<"Hello";
> > }
> > };

>
> Usually 1. Why don't you try it?


i've tried it... but why is it 1? Is it just for memory allocation so
that no two objects are allocated the same space?

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      09-05-2007
<> wrote in message
news: oups.com...
> On Sep 5, 8:51 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
>> <deepakvs...@gmail.com> wrote in message
>>
>> news: oups.com...
>>
>> > what is the size of an class with not data members?
>> > ex:
>> > class A {
>> > };

>>
>> > also
>> > sizeof ?
>> > class A{
>> > int f(){
>> > cout<<"Hello";
>> > }
>> > };

>>
>> Usually 1. Why don't you try it?

>
> i've tried it... but why is it 1? Is it just for memory allocation so
> that no two objects are allocated the same space?


Yes, among other considerations.


 
Reply With Quote
 
deepakvsoni@gmail.com
Guest
Posts: n/a
 
      09-05-2007
On Sep 5, 9:43 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> <deepakvs...@gmail.com> wrote in message
>
> news: oups.com...
>
>
>
>
>
> > On Sep 5, 8:51 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> >> <deepakvs...@gmail.com> wrote in message

>
> >>news: groups.com...

>
> >> > what is the size of an class with not data members?
> >> > ex:
> >> > class A {
> >> > };

>
> >> > also
> >> > sizeof ?
> >> > class A{
> >> > int f(){
> >> > cout<<"Hello";
> >> > }
> >> > };

>
> >> Usually 1. Why don't you try it?

>
> > i've tried it... but why is it 1? Is it just for memory allocation so
> > that no two objects are allocated the same space?

>
> Yes, among other considerations.- Hide quoted text -
>
> - Show quoted text -


so you mean to say the actual size of object is 0 but it is allocated
atleast 1 byte..

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-05-2007
On Sep 5, 5:54 am, "deepakvs...@gmail.com" <deepakvs...@gmail.com>
wrote:
> On Sep 5, 8:51 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> > <deepakvs...@gmail.com> wrote in message


> >news: roups.com...


> > > what is the size of an class with not data members?
> > > ex:
> > > class A {
> > > };


> > > also
> > > sizeof ?
> > > class A{
> > > int f(){
> > > cout<<"Hello";
> > > }
> > > };


> > Usually 1. Why don't you try it?


It's implementation defined. It's usually 1 on byte addressed
machines, but probably the same as sizeof(int) on a word
addressed machine.

> i've tried it... but why is it 1? Is it just for memory allocation so
> that no two objects are allocated the same space?


It's principally so that no two objects (of the same type) have
the same address. If you write something like:
A array[2] ;
you are guaranteed that &array[0] != &array[1].

Note that there are certain, very special cases where the
compiler is allowed to allocate less memory than the size of the
object. For example, given:

class A {} ;
class B : public A { int i ;} ;

With many compilers, sizeof( B ) == sizeof( int ), even though
sizeof( A ) + sizeof( int ) is larger. (This is called the
"empty base class" optimization.)

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      09-05-2007
<> wrote in message
news: ps.com...
> On Sep 5, 9:43 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
>> <deepakvs...@gmail.com> wrote in message
>>
>> news: oups.com...
>>
>> > On Sep 5, 8:51 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
>> >> <deepakvs...@gmail.com> wrote in message

>>
>> >>news: groups.com...

>>
>> >> > what is the size of an class with not data members?
>> >> > ex:
>> >> > class A {
>> >> > };

>>
>> >> > also
>> >> > sizeof ?
>> >> > class A{
>> >> > int f(){
>> >> > cout<<"Hello";
>> >> > }
>> >> > };

>>
>> >> Usually 1. Why don't you try it?

>>
>> > i've tried it... but why is it 1? Is it just for memory allocation so
>> > that no two objects are allocated the same space?

>>
>> Yes, among other considerations
>>

> so you mean to say the actual size of object is 0 but it is allocated
> atleast 1 byte..


No, the size of the object is 1 byte. The compiler adds a padding byte.
Read James message, an array of 0 sized objects wouldn't work.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sizeof of an instance differ from sizeof of a class GRenard C++ 2 11-03-2006 07:21 AM
sizeof an empty class object JK C++ 2 03-24-2005 12:58 PM
sizeof(Class)/sizeof(Object) Nikolai Weibull Ruby 2 12-31-2004 06:42 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments
 



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