Size of base class object and derived class object in C ++

#include <stdio.h>

class Base1
{
  public:

  virtual int virt1() { return 100; }

  int data1;
};

class Derived : public Base1
{
  public:

  virtual int virt1() { return 150; }

  int derivedData;
};

int Global1( Base1 * b1 )
{
  return b1->virt1();
}

main1()
{
  Derived * d = new Derived;

  printf( "%d %d\n", d->virt1(), Global1( d ));
  printf("size: Base1:%d\n", sizeof(Base1));
  printf("size: Derived:%d\n", sizeof(Derived));
}

      

I used the above code to print the size of the base class and derived class. I am running the code on a 64 bit machine. Logging out from my computer -

150 150
size: Base1:16
size: Derived:16

      

I also tried to print the size of the int using sizeof (int), which is 4.

I have the following questions:

  • For class size Base1, it must contain vptr points for vtable and integer data1. Why is its size 16 and sizeof (int) on my machine 4.
  • For a derived class, it must have data inherited from Base1 and an optional integer. It must be larger than Base1, why are they the same?
  • Besides the size of these, what is the vptr in the Derived class? Will the vptr in the Derived class override the vptr inherited from the Base1 class?
+3


source to share


2 answers


For class size Base1, it must contain vptr points for vtable and integer data1. Why is its size 16 and sizeof (int) 4 on my machine.

Padding to ensure correct alignment of 8-byte pointers when Base1

in a required-contiguous array.

† The C ++ standard requires array elements to be contiguous in memory and compute the memory address of the element at index i and the base address of the array plus i times the element size

For a derived class, it must have data inherited from Base1 and an optional integer. It must be larger than Base1, why are they the same?



The padding was corrected for an additional member int

. You can watch it by withdrawing d

, &d->data1

, &d->derivedData

.

Also, what is the vptr in the Derived class? Will the vptr in the Derived class override the vptr inherited from the Base1 class?

In implementations that use pointers to virtual dispatch tables (every compiler I know about, but not obligated by the C ++ standard), the class constructor Derived

and destructor overwrite "vptr" - the one who writes the pointer to Derived

VDT after starting the constructor body, the latter restores a pointer to the Base

VDT before the destructor body is executed (this ensures that you don't call Derived

member functions on the object before or after its official lifetime).

+3


source


There are many factors that determine the size of a class object in C ++.

These factors are:

  • Size of all non-static items
  • Item order
  • Byte-alignment or byte padding
  • The size of its closest base class
  • Existence of virtual functions (dynamic polymorphism using virtual functions).
  • Compiler used
  • Inheritance mode (virtual inheritance)

When I compile your code for MVSC 2010, x64 bit. This is the result of the memory layout:

class Base1 size(16):
    +---
 0  | {vfptr}
 8  | data1
    | <alignment member> (size=4)
    +---

Base1::$vftable@:
    | &Base1_meta
    |  0
0   | &Base1::virt1

      



Base1 :: virt1 this regulator: 0

   class Derived    size(24):
    +---
    | +--- (base class Base1)
0   | | {vfptr}
8   | | data1
    | | <alignment member> (size=4)
    | +---
16  | derivedData
    | <alignment member> (size=4)
    +---

Derived::$vftable@:
    | &Derived_meta
    |  0
 0  | &Derived::virt1

      

You should read more about vTable, virtual function, class / struct byte alignment. Here are some links that might help you. Hope this is helpful to you:

Determine the size of the class object: http://www.cprogramming.com/tutorial/size_of_class_object.html

Memory diagram: http://www.phpcompiler.org/articles/virtualinheritance.html

0


source







All Articles