sizeof Object without memeber variable 
Author Message
 sizeof Object without memeber variable

Hi,

Can anyone explain me the below mentioned doubts?

1.

  class  Test
 {

  public:

   void read()
   {
    cout<<"It is in read function";
   }

   void write()
   {
    cout<<"It is in write function";
   }

 };

 void main
 {
  Test a;
  cout<<sizeof(a);
 }

Question: This class doesn't have any member varialbe. I am using sizeof
function for that object. The output was 1. why its result is 1?

2.

 class Test
  {

   public:
    void virtual read()
    {
     cout<<"It is in read function";
    }

    void write()
    {
     cout<<"It is in write function";

    }
  };

 class Test1 : public Test
 {
  public:
   void read()
   {
    cout<<"It is in derived read function";
   }

 };

 void main()
 {
  Test a;
  cout << sizeof(a);
 }

Question: Class Test doesn't have any member variable. Here one virtual
function is there. the sizeof returns 4 as output. why its result is 4?

compare the program 1 and 2

please help me

Thanks in Advance
Subu



Sun, 15 Aug 2004 18:17:47 GMT  
 sizeof Object without memeber variable



Quote:
> Hi,

> Can anyone explain me the below mentioned doubts?

> 1.
>   class  Test
>  {
>   public:
>    void read()    {cout<<"It is in read function"; }
>    void write()   {cout<<"It is in write function"; }
>  };

>  void main
>  {
>   Test a;
>   cout<<sizeof(a);
>  }
> Question: This class doesn't have any member varialbe. I am using sizeof
> function for that object. The output was 1. why its result is 1?

The compiler insert a dummy member in your class so that it doesn't have a
null size. The reason is to be able to get the adress of the object. If your
class had a 0 size, the following statement would have no meaning :
Test a;
Test* pt_a=&a;

Therefore, the compiler make sure that your class is not 0 sized. If you put
any data member in your class, or even a virtual function (which would mean
a vptr - see below), the dummy member is removed.

- Show quoted text -

Quote:
> 2.
>  class Test
>   {
>    public:
>     void virtual read()  { cout<<"It is in read function"; }
>     void write()     {cout<<"It is in write function"; }
>   };

>  class Test1 : public Test
>  {
>   public:
>    void read()
>    { cout<<"It is in derived read function"; }
>  };

>  void main()
>  {
>   Test a;
>   cout << sizeof(a);
>  }

> Question: Class Test doesn't have any member variable. Here one virtual
> function is there. the sizeof returns 4 as output. why its result is 4?

A soon as you have a virtual function in a class, the compiler inserts in
that class an hidden data member, the virtual table pointer (vptr). This
pointer points to the virtual table (vtable) of the class. This
compiler-generated table holds the adresses of all the virtual functions of
the class. This is the mechanism used to perform polymorphism (calling the
right function according to the actual type of the object).

When you have something like this :
Test* pt_Test= .....
pt_Test->read();

What the compiler actually does is taking the vptr of *pt_Test, using it to
access the vtable corresponding to the real type of *pt_Test (it can be Test
or Test1), and then fetching the address of the read method that is stocked
in the vtable. This way, the right version of read is called.

There is no magic about polymorphism, just a hidden pointer (that is, 4
bytes on 32 bits platforms) that is added to each object.

Arnaud.
MVP - VC



Sun, 15 Aug 2004 18:43:08 GMT  
 sizeof Object without memeber variable

Quote:
> Question: This class doesn't have any member varialbe. I am using sizeof
> function for that object. The output was 1. why its result is 1?

According to the C++ standard, every object will have non-zero size. So if
you have a struct with no member, compiler may allocate it any non zero size
(usually 1). But if you use this struct as a base class, then compiler can
optimize to make base class size zero in the derived object.

Having a virtual function in the base class adds a vptr to the object (4
bytes in 32 bit platforms) thus making its size equal to 4 in your case.

Vishal


Quote:
> Hi,

> Can anyone explain me the below mentioned doubts?

> 1.

>   class  Test
>  {

>   public:

>    void read()
>    {
>     cout<<"It is in read function";
>    }

>    void write()
>    {
>     cout<<"It is in write function";
>    }

>  };

>  void main
>  {
>   Test a;
>   cout<<sizeof(a);
>  }

> Question: This class doesn't have any member varialbe. I am using sizeof
> function for that object. The output was 1. why its result is 1?

> 2.

>  class Test
>   {

>    public:
>     void virtual read()
>     {
>      cout<<"It is in read function";
>     }

>     void write()
>     {
>      cout<<"It is in write function";

>     }
>   };

>  class Test1 : public Test
>  {
>   public:
>    void read()
>    {
>     cout<<"It is in derived read function";
>    }

>  };

>  void main()
>  {
>   Test a;
>   cout << sizeof(a);
>  }

> Question: Class Test doesn't have any member variable. Here one virtual
> function is there. the sizeof returns 4 as output. why its result is 4?

> compare the program 1 and 2

> please help me

> Thanks in Advance
> Subu



Mon, 16 Aug 2004 00:39:25 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. sizeof without sizeof

2. MFC ClassWizard Memeber Variables

3. Vector private memeber variable in ATL dialog class

4. Interchanging values of variables without using a temporary variable

5. Basic question on sizeOf variables int, long

6. sizeof( _variable_ )

7. Auto variable with sizeof == 0

8. relation between sizeof(void*), sizeof(int)

9. sizeof (struct) ! = sizeof struct elements

10. sizeof a/sizeof *a

11. sizeof(foo) -1 or sizeof(foo)

12. sizeof(void*) versus sizeof(foo*)

 

 
Powered by phpBB® Forum Software