Memory layout changed for classes with virtual functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello

we are changing our compiler from gcc version 2.95.3 to a more recent
version (gcc version 3.4.6).
What we have observed is that the memory layout of C++ objects with
virtual functions has been changed.
In the old compiler version the virtual table pointer was at the end
of the object, while in the new compiler version it is in the
beginning of the object.

An example:

class A {
public:
  A() : next(0),a(1) {}
  virtual void func() {}
  xls_list next;
  int a;
};

int main(void)
{
  A a;
  return 0;
}

In 2.95.3 this looks like:
(gdb) print a
$1 = {next = 0x0, a = 1, _vptr. = 0x27068}

While if compiled with 3.4.6:
(gdb) print a
$1 = {_vptr.A = 0x106d0, next = 0x0, a = 1}

In a lot of places in our software we use macros to put elements in a
linked list.
We have made sure that the "next" member is always the first member of
the class and we use the macros to manage a linked list of objects.
(In essence the xls_list object just contains a next pointer). I know,
this is not clean but works well in the 2.95.3 compiler.

So this is how our application does look like:
   A a;
   xls_create(&list);
   xls_addhead(&list, &a);

   A *aptr = 0;
   if (xls_head(&list, (void**)&aptr)) {
     aptr->funcA();
   }

Now that we are moving to a newer version, our SW is broken since the
virtual table ptr is stored as first entry in the object's memory.
As soon as we insert an object in a list, we simply corrupt the VTABLE
pointer and the SW soon crashes.

Now my questions are:
- is there a kind of mode / compiler option to make the new compiler
use the old memory layout?
- does somebody know a solution (except rewriting the complete code base) ?

Any help is much appreciated.

Thanks
Ronny


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux