Freaks am Deepchand

I was working in Visual studio 6 till a month ago now am in VS 2005. The problem is i had a concept regarding the virtual address table i.e. end of virtual address table is indicated by 0 or NULL.

If u people run this program in VC 6 and VC8 outputs are different. please clarify me y the output in VC 8 is different from VC 6

#include <iostream>

using namespace std;

class Class {

virtual void f() { cout << "Class::f" << endl; }

virtual void g() { cout << "Class::g" << endl; }

};

int main() {

Class objClass;

cout << "Address of virtual pointer " << (int*)(&objClass+0) << endl;

cout << "Value at virtual pointer i.e. Address of virtual table "

<< (int*)*(int*)(&objClass+0) << endl;

cout << endl << "Information about VTable" << endl << endl;

cout << "Value at 1st entry of VTable "

<< (int*)*((int*)*(int*)(&objClass+0)+0) << endl;

cout << "Value at 2nd entry of VTable "

<< (int*)*((int*)*(int*)(&objClass+0)+1) << endl;

cout << "Value at 3rd entry of VTable "

<< (int*)*((int*)*(int*)(&objClass+0)+2) << endl;

cout << "Value at 4th entry of VTable "

<< (int*)*((int*)*(int*)(&objClass+0)+3) << endl;

return 0;

}

One more doubt did u people introduce sealed keyword of C# to C++



Re: Visual C++ Language Some 1 please clarify this

Stephen Lim

In visual studio 2003 +, or any .NET C++ compiler, the Virtual Table is not guaranteed to be null-terminated. I dont know if it was in VC6.

Also in terms of reading from the table I have a difficult time following how exactly your achieving this. It is much easier if you:

Code Snippet

( (DWORD*)(objClass))[0..1..2..3..4]

Address it in terms of an array. This eliminates possible errors with the way your fetching data using pointers, as you could be pointing to the middle of an integer for all you know.

Furthermore when you compile it, the addresses of your compiled functions are not guaranteed to be the same as the addresses of your compiled functions in MSVC6. You must verify whether this is the case or not using some sort of debugger.





Re: Visual C++ Language Some 1 please clarify this

Carl Daniel

There's no guartantee that the v-table is null terminated and there never has been. With VC6 you just got "lucky" (probably something having to do with linker placement of v-tables such that they're almost always null-padded). Even if it was null terminated, there's no guarantee that the functions are placed in the table in the same order, or even that they're at positive offsets from the "start" of the table.

According to the C++ standard, there's not even a guarantee that there is such a thing as a v-table, nor that if a v-table exists that it can be located in any predictable way.