[Bug c++/14808] New: Undefined results with virtual base classes
dannysmith at users dot sourceforge dot net
gcc-bugzilla@gcc.gnu.org
Thu Apr 1 09:05:00 GMT 2004
With both 3.4.0 (20040327) and trunk (20040331), I am getting segfaults
or unexpected results with virtual base classes on i386-pc-mingw32
Here is an illustration:
// vbase.cc
#include <stdio.h>
class Base
{
public:
// int base_var;
virtual char* vf() const = 0;
virtual ~Base() {}
};
class D1 : public Base
{
public:
char* vf() const { return "D1"; }
};
class D2 : virtual public Base
{
public:
char* vf() const { return "D2"; }
};
void test1()
{
Base* b1 = new D1;
printf("%s\n", b1->vf());
}
void test2()
{
Base* b2 = new D2;
printf("%s\n", b2->vf());
}
int main()
{
test1();
test2();
return 0;
}
//end vbase.cc
With trunk:
When compiled without optimisation
g++ -ovbase0 -Wall -O0 vbase.cc
I get successful build, with no warnings. But executing vbase0 causes
segfault.
Ditto with -O1
When compiled with -O2 optimisation
g++ -ovbase2 -Wall -O2 vbase.cc
executing vbase2 outputs:
D1
2D2
When I add -g:
g++ -ovbase0g -g -Wall -O0 vbase.cc
Executing vbase0g doesn't segfault. But it doesn't produce _any_
output either. Ditto with -O1 -g.
This is regression from 3.3.3 which output
D1
D2
at all levels of optimisation
2.95.3 also produced the correct output.
If I add a variable (the commented out int base_var) to the
base class, the executable built with 3.4.0 and trunk executes
without problem and produces the correct output.
If I comment out the "virtual" keyword in D2's definition, I
also get correct output with 3.4.0 and trunk.
Danny
--
Summary: Undefined results with virtual base classes
Product: gcc
Version: 3.4.0
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: dannysmith at users dot sourceforge dot net
CC: gcc-bugs at gcc dot gnu dot org
GCC build triplet: i686-pc-mingw32
GCC host triplet: i686-pc-mingw32
GCC target triplet: i686-pc-mingw32
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14808
More information about the Gcc-bugs
mailing list