简体   繁体   中英

The 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 out the size of the base class and derived class. I am running the code in a 64 bit machine. The output from my computer is

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

I also tried to print out the size of int by using sizeof(int), it's 4.

I have the following questions:

  1. For the size of Base1 class, it should contain a vptr points to the vtable and an integer data1. Why its size is 16 while sizeof(int) is 4 in my machine.
  2. For the derived class, it should have the data inherited from Base1 class and an additional integer. It should have bigger size than Base1 class, why they are the same?
  3. Besides the size of these, what's the vptr in Derived class? Will the vptr in Derived class override the vptr inherited from the Base1 class?

For the size of Base1 class, it should contain a vptr points to the vtable and an integer data1. Why its size is 16 while sizeof(int) is 4 in my machine.

Padding to ensure proper alignment of the 8-byte pointers when Base1 s are in a necessarily-contiguous† array.

† the C++ Standard requires that array elements be contiguous in memory, and calculates the memory address of an element at index i and the array's base address plus i times the element size

For the derived class, it should have the data inherited from Base1 class and an additional integer. It should have bigger size than Base1 class, why they are the same?

The padding's been reclaimed for the extra int member. You can observe this by outputting d , &d->data1 , &d->derivedData .

Besides the size of these, what's the vptr in Derived class? Will the vptr in Derived class override the vptr inherited from the Base1 class?

In implementations using pointers to virtual dispatch tables (every compiler I know of, but it's not mandated by the C++ Standard), the Derived class constructor(s) and destructor overwrites the "vptr" - the former writing a pointer to Derived 's VDT after the constructor body runs, the latter restoring the pointer to the Base VDT before the destructor body runs (this ensures you don't invoke Derived member functions on an object before or after its official lifetime).

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

These factors are:

  • Size of all non-static data members
  • Order of data members
  • Byte alignment or byte padding
  • Size of its immediate base class
  • The existence of virtual function(s) (Dynamic polymorphism using virtual functions).
  • Compiler being used
  • Mode of inheritance (virtual inheritance)

When I compile your code on MVSC 2010, x64 bits. This is the result of 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 adjustor: 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, byte alignment of class/struct. Here, some links can help you. Hope it helpful to you:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM