This is the mail archive of the gcc-bugs@gcc.gnu.org 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]

bug in dynamic_cast - incorrect pointer returned


The program below illustrates the problem. Basically the pointer returned from a
dynamic cast is out by 4 bytes.
The problem appears both on solaris and on NT:

Version information (solaris):
g++ -v
Reading specs from
/home/shared/kits/installed/gcc-2.95.2/lib/gcc-lib/sparc-sun-solaris2.5.1/2.95.2/specs

gcc version 2.95.2 19991024 (release)

and NT:
Reading specs from /usr/lib/gcc-lib/i686-pc-cygwin/2.95.2-5/specs
gcc version 2.95.2-5 19991024 (cygwin experimental)

Compilation command: g++ -g -Wall tester.cpp

Is there a patch available to fix this problem? I tried to browse through the
archives but it's clearly a complex area.
Thanks,
Mark.

/******************** program ***************/
#include<stdio.h>

class ExtraInterface {
public:
    virtual void convert() = 0;
};

class VirtualBase{
public:
    virtual ~VirtualBase(){}
};

class Base: public virtual VirtualBase{};

class Derived1: public Base, public ExtraInterface{
public:
    virtual void convert() {}
};


class Derived2: public Derived1{};

int main(){
    Derived2   derived;
    Derived2*  derivedPtr = &derived;
    Derived1*  basePtr = derivedPtr;
    Derived2*  derivedPtr2 = dynamic_cast<Derived2*>(basePtr);
    if (derivedPtr2 != derivedPtr){
        printf("bug casting from Derived1\n");
    } else {
        printf("ok casting from Derived1\n");
    }
    return 0;
}

/*************** end of program ********************/



This communication is for informational purposes only.  It is not intended as
an offer or solicitation for the purchase or sale of any financial instrument
or as an official confirmation of any transaction. All market prices, data
and other information are not warranted as to completeness or accuracy and
are subject to change without notice. Any comments or statements made herein
do not necessarily reflect those of J.P. Morgan Chase & Co., its
subsidiaries and affiliates.


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