0

I read this question and some others regarding the layout of on object, but I still don't get exactly how it looks like.

Here are my specific question:

For each class (meaning that if I have a 2 super-classed I would have 2 pointers), virtual functions have 1 vtable pointer for them. where is it inside the object? Assuming the following: class A{void virtual f(){}; int x;}; would the address of an object A a be the same as the address of a.x or of a.f [or maybe point to a default method such as the C-tor / D-tor Incorrect, as class methods are not stored inside the object as explained here]

Example:

    class A{
    int x;
    void f(){}
    void virtual g(){}
    void virtual h(){}
};

A a;
std::cout << sizeof a; // result = 8

class A{
    int x;
    void f(){}
    void virtual g(){}
};

A a;
std::cout << sizeof a; // result = 8

class A{
    int x;
    void f(){}
    //void virtual g(){}
};

A a;
std::cout << sizeof a; // result = 4

From these examples it can be seen that when encountering a number (n > 0) of virtual functions, a pointer (4-bytes, on my 32-bit machine) is added to the object. Would it be added before other data members?

What would be pointed by:

A a;
int *p = (int*)&a;

I checked it out with this. Is it right to assume from the following that the vtable pointer always precedes other class members?:

class A{
public:
    A(int y){x=y;}
    virtual void g(){}
    int x;
    virtual void f(){}
};

int main ()
{
    A a(42);
    int *p = (int*)&a;
    std::cout << *p << std::endl;      // = 4215116 (vtable address?)
    std::cout << *(p+1) << std::endl;  // = 42
    return 0;
}
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
  • @DanielLangr yeah, I edited my Q with a link to an answer that says that. Forgot do delete that sentence as well. – CIsForCookies Oct 09 '18 at 05:17
  • @1201ProgramAlarm it is indeed similar, but the answer there is somewhat unclear – CIsForCookies Oct 09 '18 at 05:19
  • @CIsForCookies: "*Is it right to assume from the following that the vtable pointer always precedes other class members?*" You can't even assume that vtables are things that *exist* in C++ implementations. Sure, most compilers do use them as their means of implementing virtual functions. But there is no guarantee of that. – Nicol Bolas Oct 09 '18 at 05:30
  • 1
    `int *p = (int*)&a;` This is undefined behavior I guess in case that `A` has virtual functions. You can do this only for [standard-layout classes](https://en.cppreference.com/w/cpp/language/data_members). – Daniel Langr Oct 09 '18 at 05:33
  • @NicolBolas so basically, in classes that have virtual methods, trying to find a specific member based on the objects address and offsets is doomed to be UB or at least Implementation dependent? – CIsForCookies Oct 09 '18 at 05:37

1 Answers1

2

This is purely implementation dependent(compilers) and most of the implementations tend to go with inserting vptr as the first element. Since it is the first element and beginning of the object address, indirection for the virtual function call invocation will be easier as there is no further offset calculation to identify the vptr. Similar questions be asked in stackoverflow before and found the below one is useful. Why is vptr stored as the first entry in the memory of a class with virtual functions?

Jolly Jose
  • 84
  • 5
  • How much this **implementation dependent** can vary? I mean, as I see it, it *should* (meaning, this is how I would have done it ;-) ) sit in the object's head/tail. Is it "legal" to implement it, say, like non-virtual methods, i.e in the code segment or something crazy like this, or there are some guidelines? – CIsForCookies Oct 09 '18 at 05:26
  • 1
    Standard committee might have defined some guidelines, will provide the link soon. – Jolly Jose Oct 09 '18 at 05:28
  • This is an excerpt from the book Inside C++ Object Model. – Jolly Jose Oct 09 '18 at 05:48
  • 1
    by Stanley B Lippman. "Subsequent to Release 2.0, with its addition of support for multiple inheritance and abstract base classes, and the general rise in popularity of the OO paradigm, some implementations began placing the vptr at the start of the class object. " – Jolly Jose Oct 09 '18 at 05:56