Detail about How VPTR and Virtual table works

Home Forums C Programming Detail about How VPTR and Virtual table works

Viewing 3 reply threads
  • Author
    Posts
    • #2072
      shalizy
      Participant

      Assumption: 32-bit Machine.
      Here I am going to explain how Virtual table, Virtual pointer for Virtual functions are internally working.

      First we have understand memory layout.
      Example 1: How the class’s memory layout
      Code: cpp

      OUTPUT:
      obj’s Size = 8
      obj ‘s Address = 0012FF7C

      Note: Any Plane member function does not take any memory.

      Example 2: Memory Layout of Derived class

      OUTPUT:

      obj1’s Size = 8
      obj1’s Address = 0012FF78
      obj2’s Size = 12
      obj2’s Address = 0012FF6C

      Example 3: Memory layout If we have one virtual function.
      Code: cpp

      OUTPUT:
      obj’s Size = 8
      obj’s Address = 0012FF7C

      Note: Adding one virtual function in a class takes 4 Byte extra.

      Example 4: More than one Virtual function
      Code: cpp

      OUTPUT:
      obj’s Size = 8
      obj’s Address = 0012FF7C

      Note: Adding more virtual functions in a class, no extra size taking i.e. Only one machine size taking(i.e. 4 byte)

      Example 5:
      Code: cpp

      OUTPUT:
      a = 200
      b = 10

      If we Change the code as then

      Code: Cpp // Changing a and b

      OUTPUT:
      a = 100
      b = 200

      Note: Who sits 1st place of Class : Answer is VPTR
      VPTR – 1st placed in class and rest sits after it.

      Example 6:
      Code: cpp

      OUTPUT:

      VPTR’s Address 0012FF7C
      VPTR’s Value 0046C060

      NOTE: This VPTR’s value is a address of Virtual table. Lets see in next Example.

      Example 7:

      OUTPUT:
      VPTR’s Address 0012FF7C
      VIRTUAL TABLE ‘s Address 0046C0EC
      Value at first entry of VIRTUAL TABLE 0040100A
      Test: fun1

      Example 8:

      OUTPUT:
      VPTR’s Address 0012FF7C
      VIRTUAL TABLE ‘s Address 0046C0EC
      Value at first entry of VIRTUAL TABLE 0040100A
      Value at 2nd entry of VIRTUAL TABLE 004012

      Example :9

      OUTPUT:
      Test::fun1
      Test::func1

      Example 10: multiple Inheritance

      OUTPUT:
      Derive’s Size = 12

      Example 11: Calling Virtual Functions in case of Multiple Inheritance

      OUTPUT:
      Base1::fun
      Base1::func
      Base2::fun
      Base2::func
      Base3::fun
      Base3::func
      Drive::Fn
      Drive::Fnc

    • #3343
      deepaksancheti
      Participant

      I am sorry to say…
      But you r wrong at the very first example….

      It will simply occupy only 4 bytes…..
      How come 8 bytes?????

    • #3344
      Anonymous
      Inactive

      int data1 has 4 byte
      and
      int data2 has 4 byte

      then total 8 byte

    • #3345
      Adetutu
      Participant

      Hi,

      Please confirm on which platform you are testing your program. On TurboC3 it has some diffrent output. I hope You are using gcc compiler on linux or solaris
Viewing 3 reply threads
  • You must be logged in to reply to this topic.
Share
Tweet
Share
Pin