Where does the Intel C ++ compiler store vptr (a pointer to a table of virtual functions) in an object?

Where does the Intel C ++ compiler store vptr (a pointer to a table of virtual functions) in an object?

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

+3
source share
5 answers

For the Intel C ++ compiler for Linux, I found this to be the beginning of an 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]);
}
+1
source

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

#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();
}

In my Linux window, the result is:

bfdbef3c bfdbef40

... this is exactly what you expect: the first pointer plus the size of the virtual function pointer is the location of the second.

+3
source

" "? Windows vptr 0 , Unix . Intel Windows Unix, , Win vptr * nix .

0

vtable . .

, ICC, - .

0

Except for your curiosity, the real answer to your question will not be very useful, since the C ++ standard does not define such details. Therefore, any compiler provider can implement these functions at its discretion. This is one of the reasons that for C ++ there is no cross-platform ABI (application binary interface).

0
source

Source: https://habr.com/ru/post/1705747/


All Articles