This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Object layout bug with explicit specialization

Hi there,

after some consideration, we decided that this must be a bug in the
compiler: when deriving from explicitely specialized classes, the compiler
computes wrong offsets (relative to #this#) for member variables of the
base classes. The result is that a variable has the right value when
accessed in the scope of the base class, but produces a wrong value when
accessed in the scope of the derived class. The following short program
should make this clear.

1/ The problem stems from the fact that FinalClass<1> is derived from the
explicitely specialized class Derived<1>, rather than from the general
Derived<dim> template. The compiler seems to ignore this, however, when
computing the addresses of #local[12]# in FinalClass<1>::FinalClass, since
both addresses equal #this#. It is amazing that the compiler even knows
about #local[12]#, if it does not know about the exact class hierarchy,
since Derived<dim> is not derived from Base[12] and thus has no members
#local1# and #local2#.

This confusion about base classes can better be seen when declaring the
general Derived<dim> class like this:
  template <int dim>
  class Derived : public Base1, public Base2 {};
The output of the program then is correct. It is amazing that the
addresses are still right, if Derived<1> is derived from Base1 and Base2,
while Derived<dim> is derived from Base2 and Base1 (note the order).

2/ The compiler throws an internal compiler error when any of the two base
classes of Derived<1> are declared virtual.

3/ The described error shows up with gcc 2.8.0 and 2.8.1, as well as with
egcs 2.91.6 (98/01/22) and 1.0.2. The program generated with gcc
is correct. The problem has also been reported to the gcc folks.

4/ I am surprised to see that this error seems not yet to have been
reported, since explicit specialization of classes is not so uncommon
today. This error is so serious, that it should make all programs produce
invalid results which derive from explicitely specialized classes :-(
Anyway, I'm sure you will find what needs to be changed and my hopes are
high that things will be ok again earlier than the next main gcc


---------------------- Sample output ------------------------------------

---------------------- Sample code ------------------------------------
#include <iostream.h>

// print the function's name, and addresses of local variables
#define PF     cout << __PRETTY_FUNCTION__ << ":" << endl
#define P1     cout << "    &local1=" << &local1 << endl
#define P2     cout << "    &local2=" << &local2 << endl

class Base1 {
    Base1 () : local1(0)  { PF; P1; };
    int local1;

class Base2 {
    Base2 () : local2(0) { PF; P2; };
    int local2;

template <int dim>
class Derived {};

template <>
class Derived<1> : public Base1, public Base2 {
    Derived () { PF; P1; P2; };

template <int dim>
class FinalClass :  public Derived<dim> {
    FinalClass () { PF; cout << "    this=" << this << endl; P1; P2; };

void main () {
  FinalClass<1> a1;

----------------------- Compiler options for egcs 1.0.2 ---------------
wolf@eris$ /usr/local/egcs/egcs-1.0.2/bin/g++ -v -o x
Reading specs from
gcc version egcs-2.90.27 980315 (egcs-1.0.2 release)

-lang-c++ -v -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus
-D__GNUC_MINOR__=90 -Dsparc -Dsun -Dunix -D__svr4__ -D__SVR4 -D__sparc__
-D__sun__ -D__unix__ -D__svr4__ -D__SVR4 -D__sparc -D__sun -D__unix
-Asystem(unix) -Asystem(svr4) -D__EXCEPTIONS -D__GCC_NEW_VARARGS__
-Acpu(sparc) -Amachine(sparc) /var/tmp/ccaOaRL_.ii
GNU CPP version egcs-2.90.27 980315 (egcs-1.0.2 release) (sparc)
#include "..." search starts here:
#include <...> search starts here:

End of search list.

/var/tmp/ccaOaRL_.ii -quiet -dumpbase -version -o
GNU C++ version egcs-2.90.27 980315 (egcs-1.0.2 release)
(sparc-sun-solaris2.6) compiled by GNU C version 2.8.1.
 /usr/ccs/bin/as -V -Qy -s -o /var/tmp/ccaOaRL_1.o /var/tmp/ccaOaRL_.s
/usr/ccs/bin/as: WorkShop Compilers 4.X dev 18 Sep 1996

-V -Y P,/usr/ccs/lib:/usr/lib -Qy -o x
-L/usr/local/egcs/egcs-1.0.2//sparc-sun-solaris2.6/lib -L/usr/ccs/bin
-L/usr/ccs/lib -L/usr/local/egcs/egcs-1.0.2//lib /var/tmp/ccaOaRL_1.o
-lstdc++ -lm -lgcc -lc -lgcc
ld: Software Generation Utilities - Solaris/ELF (3.0)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]