tags:

views:

591

answers:

5

Where does Intel C++ Compiler store the vptr ( pointer to virtual function table ) in an Object ?

I believe MSVC puts it at the beginning of the object, gcc at the end. What is it for icpc ( Intel C++ Compiler )?

A: 

What do you mean "at the beginning of the function"? Anyway, Windows compilers put vptr at the offset 0 of an object, while Unix compiler put it after the last member data. Intel produces compilers for both Windows an Unix, and I would expect that the Win version puts vptr at the beginning, and the *nix version at the end of the object.

Nemanja Trifunovic
A: 

The position of the vtable pointer in the object is not related to the operating system. It's only a convention of the compiler and other tools.

Sid Datta, you can discover that information from ICC disassembling some compiled module.

GogaRieger
+1  A: 

You can always look at where this and the first address in the derived class are located:

#include <stdio.h>

class foo
{
public:
    virtual int do_foo()=0;
};

class bar : public foo
{
public:
    int bar;
    int do_foo() {printf ("%08x %08x\n", this, &bar);}
};

int main()
{
    bar a_bar;
    a_bar.do_foo();
}

On my linux box, the result is:

bfdbef3c bfdbef40

...which is exactly what you would expect: the first pointer, plus the size of a virtual function pointer, is the location of the second.

Why is it not a bug that that program compiles? (The virtual do_foo function is not defined in the 'bar' class) The bar class does indeed have a 'do_foo' member, but it is not virtual.
Arafangion
A: 

For Intel C++ compiler, for Linux, I found it to be the beginning of the object.

Code:

#include <cstdio>

class A 
{
  int a, b;
public:
  A(int a1, int b1): a(a1), b(b1) {}
  virtual void p(void) { printf("A\n"); }
};

class B: public A
{
public:
  B(int a1, int b1): A(a1, b1) {}
  void p(void){ printf("B\n"); }
};

int main(void)
{
  A a(1, 2); int p=10; A a1(5, 6);
  B b(3, 4); int q=11; B b2(7, 8);

  a.p();
  b.p();

  int * x=(int*)&a;
  printf("%d %d %d %d\n", x[0], x[1], x[2], x[3]);
  x=(int*)&b;
  printf("%d %d %d %d\n", x[0], x[1], x[2], x[3]);
}
Sid Datta
A: 

Except to satisfy your curiosity the real answer to your question is not going to be very useful, as the C++ standard does not define details like this. Therefore any compiler vendor can implement these features as they see fit. This is one of the reasons why there is no cross platform ABI (application binary interface) for C++.

lothar
I understand, which is why I specified Intel c++ ( icpc ) compiler.However, I have found the answer, so its all good! Thanks anyways :)
Sid Datta