[Bug c++/14827] New: Run time error, breaking of type info and static casts

Igor dot Smirnov at cern dot ch gcc-bugzilla@gcc.gnu.org
Fri Apr 2 22:57:00 GMT 2004


Hello.

Developing very large program I met a problem with covariant return types,
which I tried to bypass with void return types, but did not achieve success due
to a bug in g++. Fortunately, I managed to reproduce the most of the effects in
very little example, which I am sending you. But please be aware that similar
problems perhaps appear at different class configurations, not only at this one.
(I know because in my real program configuration is little bit different). 

I have tested this example at two different sites with two compilers (both - PC
Pentium 4 with RedHat):

First:

Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --host=i386-redhat-linux --with-system-zlib --enable-__cxa_atexit
Thread model: posix
gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)

Second:

Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.3 2.96-113)

The output of the both cases is the case:

SuperDerived::print: Hello
SuperDerived::fun: Hello
SuperDerived::fun: Hello
SuperDerived::fun: Hello

Here only the first line is correct. Three the others are not.
In all the cases one should expect the reply from function print() like
occured in the first line. But the type information is somehow broken
during the static casts, which are expected to affect the type assigned by
compiler to the pointer, but not the type of the object, by my understanding. 
This violation results in involving different function of the class. In my real
program this results in skipping the print function at all and in segmentation
fault a few lines later.

Some comments on how static casts are extected to function can be obtained in B.
Stroustrup, The C++ Programming Language, Special edition, Addison-Weley, p. 413.
  
I would appreciate if you let me know whether this problem has already been
corrected in newer release of g++, or not, or can it be corrected at all. Are
there ways to bypass it. By my opinition this is a real and very serious
problem. Thank you.

Regards

    Igor Smirnov


The code:


#include <iostream.h>

class Base1
{public:
  int j;
  virtual void print(void) { cout<<"Base1::print: Hello\n";}
};
class Base2
{public:
  int k;
  virtual void fun(void) = 0;
  virtual void print(void) { cout<<"Base2::print: Hello\n";}
};

class Derived: public Base1
{public:
  int l;
  virtual void print(void) { cout<<"Derived::print: Hello\n";}
};

class SuperDerived: public Base2, public Derived
{public:
  int m;
  virtual void fun(void) { cout<<"SuperDerived::fun: Hello\n";}
  virtual void print(void) { cout<<"SuperDerived::print: Hello\n";}
};

int main(void)
{
  SuperDerived sd;
  Derived* ad = new SuperDerived(sd);
  ad->print();
  ad = ( Derived* ) ( (void*) new SuperDerived(sd) );
  ad->print();
  ad = static_cast< Derived* > ( (void*) new SuperDerived(sd) );
  ad->print();
  ad = static_cast< Derived* > 
    ( static_cast< void* > ( new SuperDerived(sd) ) );
  ad->print();
}

-- 
           Summary: Run time error, breaking of type info and static casts
           Product: gcc
           Version: 3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Igor dot Smirnov at cern dot ch
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14827



More information about the Gcc-bugs mailing list