views:

261

answers:

4

Hello. I would like to ask you on what does the offset of the table of virtual functions for a class depend? I mean, from what I've read it at least depends on compiler, but does it varies from class to class?

Edit: by offset I mean the position of the table relative to the address of the owner object.

Edit: example code:

void **vtable = *((void***)(((char*)object)+offset));

int **ivtable=(int **)vtable;

void* firstFunction = (void*) ivtable[0];
A: 

Check the question V-table thunk

mloskot
+1  A: 

As it turns out, most/all C++ compilers on the Windows platform use the same virtual function table layout. The designers of Microsoft's COM technology took advanage of this fact so that C++ implementations of COM interfaces can be compiled on all the standard Windows C++ compilers.

I don't have the exact order memorised, but it basically comes down to depth-first left recursion on the object graph, so that the first declared virtual method on the left-most (1st-declared if multiple inheritance is used), deepest derived class is the first virtual method in the table. I don't know if "virtual" inheritance (using the virtual keyword in the declaration of the base class) is standardized, though.

David Gladfelter
From what I've saw in a library that deals with virtual functions hooking, in Windows the offset is always 0. But, in Linux, (that I also need) it varies. What i want to know is if in the same library all the classes have the same offset or not.
ISTR GCC puts the vtable at the _end_ of an object, so not at a fixed offset to the _start_ of an object.
MSalters
I've already tested and saw that it is located at a fixed offset.
A: 

It varies from class to class.

+1  A: 

There is certainly a dependency on the exact class.

Remember that C++ has multiple inheritance (MI). The consequence of MI is that a single object may have multiple base subobjects. Those of course cannot be at the same address. This also means that some base subobjects don't actually start at relative offset 0.

Now, this MI introduces quite a bit of complexity with vtables: you inherit functions from multiple bases, at different offsets. For that reason it's quite common to use different vtable layouts for MI classes.

On a related note, MI also means that not every pointer to an object is actually a pointer to the start of that object. It is quite likely that a SecondBase* pointer to a Derived object is offset by sizeof(FirstBase), i.e. points somewhere in the middle of the Derived object.

MSalters
Thanks. In my case I don't know of any class that inherits from more than one so I think it's ok to assume a fixed offset.