locked
VTABLE Memory Location RRS feed

  • Question

  • Hi,

       when VTABLE is get created in Memory , In compiletime or runtime?

    If the class contain one virtual function , then what is the size of class?

    Regards,

    Ranjith

    Monday, December 24, 2012 7:33 AM

Answers

  • "when VTABLE is get created in Memory , In compiletime or runtime?"

    Compile time. The vtable is simple an array of function pointers that gets stored in the executable. It never changes and this array is shared by all instances of a given class. Each instance of a class that has virtual methods contains one vtable pointer (for single inheritance), usually stored at offset 0, before all data members. This pointer is initialized at construction time to point to the vtable for that class.

    "If the class contain one virtual function , then what is the size of class?"

    The class has the same size if it has 1 or 100 virtual functions, size of the class data members + size of the vtable pointer (4 bytes for x86, 8 bytes for x64) (for single inheritance).

    • Marked as answer by Damon Zheng Monday, December 31, 2012 5:56 AM
    Monday, December 24, 2012 7:59 AM
  • "Heap or Stack?"

    As I said, the vtable is stored in the executable. The executable is memory mapped so basically it's stored in the same place where the code is, in memory that belongs to a memory mapped section, that's neither heap nor stack.

    • Marked as answer by Damon Zheng Monday, December 31, 2012 5:56 AM
    Monday, December 24, 2012 9:34 AM

All replies

  • "when VTABLE is get created in Memory , In compiletime or runtime?"

    Compile time. The vtable is simple an array of function pointers that gets stored in the executable. It never changes and this array is shared by all instances of a given class. Each instance of a class that has virtual methods contains one vtable pointer (for single inheritance), usually stored at offset 0, before all data members. This pointer is initialized at construction time to point to the vtable for that class.

    "If the class contain one virtual function , then what is the size of class?"

    The class has the same size if it has 1 or 100 virtual functions, size of the class data members + size of the vtable pointer (4 bytes for x86, 8 bytes for x64) (for single inheritance).

    • Marked as answer by Damon Zheng Monday, December 31, 2012 5:56 AM
    Monday, December 24, 2012 7:59 AM
  • Thanks................

    and where the VTABLE get stored in memory? Heap or Stack?

    Monday, December 24, 2012 9:26 AM
  • "Heap or Stack?"

    As I said, the vtable is stored in the executable. The executable is memory mapped so basically it's stored in the same place where the code is, in memory that belongs to a memory mapped section, that's neither heap nor stack.

    • Marked as answer by Damon Zheng Monday, December 31, 2012 5:56 AM
    Monday, December 24, 2012 9:34 AM
  • Thanks.........

    Monday, December 24, 2012 10:40 AM
  • And one more thing initialization of v-table  take place under default constructor.The vtable is built at compile time( statically allocated).Each of the type instances contains a pointer to this table Because this pointer is initialized at construction time, a virtual member function should never be called from the constructor

    Thanks


    Rupesh Shukla



    • Edited by Pintu Shukla Thursday, December 27, 2012 7:52 AM
    Thursday, December 27, 2012 7:47 AM
  • "And one more thing initialization of v-table  take place under default constructor."

    Strictly speaking what the constructor initializes is the vtabe pointer of the object, not the vtable itself.

    Thursday, December 27, 2012 7:51 AM
  • "And one more thing initialization of v-table  take place under default constructor."

    Strictly speaking what the constructor initializes is the vtabe pointer of the object, not the vtable itself.

    Not sure what You mean .Lets explore it little bit more as per my understanding V table initialization totally depend on OS.

    The typical compiler "Compilers may implement virtual behavior any way they want, but the way it’s described here is an almost universal approach" creates a single table (called the VTABLE) for each class that contains virtual functions. The compiler places the addresses of the virtual functions for that particular class in the VTABLE. In each class with virtual functions, it secretly places a pointer, called the vpointer (VPTR), which points to the VTABLE for that object. When you make a virtual function call through a base-class pointer (that is, when you make a polymorphic call), the compiler quietly inserts code to fetch the VPTR and look up the function address in the VTABLE, thus calling the correct function and causing late binding to take place.


    All of this – setting up the VTABLE for each class, initializing the VPTR, inserting the code for the virtual function call – happens automatically, so you don’t have to worry about it. With virtual functions, the proper function gets called for an object, even if the compiler cannot know the specific type of the object.

    By Above approach i said initialization of a vtable take place in default constructor (Now its upto us how we want to use these word to me it's more sense because after that i can explain about v pointer and offset if someone interested).

    Thanks


    Rupesh Shukla

    Thursday, December 27, 2012 8:22 AM
  • "By Above approach i said initialization of a vtable take place in default constructor"

    Which is incorrect unless you're confusing the vtable pointer with the vtable. But you don't, your edited post says:

    "And one more thing initialization of v-table  take place under default constructor.The vtable is built at compile time( statically allocated)."

    The constructor runs at run time but at the same time you're saying that the vtable is build at compile time. Those 2 statements of yours can't be true at the same time. You're playing with words.

    Thursday, December 27, 2012 8:38 AM
  • OK . As per my knowledge if you remove vptr forget about offset then there is nothing left in vtable that's why i said initialization. What else we have inside vtable to initialize anyway if u think vpointer is more suitable then fine .Have no problem in any case

    Thanks


    Rupesh Shukla

    Thursday, December 27, 2012 10:25 AM