c++/7288: Base and derived class don't agree on base class variables
jolley@fltsim.stl.mo.boeing.com
jolley@fltsim.stl.mo.boeing.com
Fri Jul 12 12:48:00 GMT 2002
>Number: 7288
>Category: c++
>Synopsis: Base and derived class don't agree on base class variables
>Confidential: no
>Severity: critical
>Priority: medium
>Responsible: unassigned
>State: open
>Class: wrong-code
>Submitter-Id: net
>Arrival-Date: Fri Jul 12 12:46:01 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator: Tom Jolley
>Release: gcc version 3.0.2 20010905 (Red Hat Linux 7.1 3.0.1-3)
>Organization:
>Environment:
Red Hat 7.2 with dual Pentium III processors.
>Description:
The derived class, with a virtual function, is derived
from shm_object. The shm_object class overrides the new
operator. One of the arguments in the overriden new
operator function is stored in the shm_object.arena
variable. The debugger shows that the value stored is
showing up in another variable of the derived class,
_vptr.derived. Here is what the debugger says when in the
derived constructor:
(gdb) print *this
$8 = {<shm_object> = {arena = 0x0}, _vptr.derived = 0x1, derived_variable = 0}
>How-To-Repeat:
#include <iostream>
using namespace std;
class shm_object {
public:
void* operator new( size_t size, void *mem_arena ) {
void*ptr;
ptr = (void*)new char[size];
if( ptr ) ((shm_object*)ptr)->arena = mem_arena;
return ptr;
}
void* operator new( size_t size ) {
void*ptr;
ptr = (void*)new char[size];
if( ptr ) ((shm_object*)ptr)->arena = NULL;
return ptr;
}
void operator delete( void *v_ptr ) {
delete [] (char*)v_ptr;
}
void* arena;
};
class derived : public shm_object {
public:
derived() {}
virtual ~derived() {}
virtual int dummy() { return 0; };
int derived_variable;
};
int main( int argc, char* argv[] )
{
derived* d;
d = new( (void*)1 ) derived;
if( d->arena != (void*)1 ) {
cout << "Arena is invalid" << endl;
} else {
cout << "Arena worked" << endl;
}
delete d;
return 0;
}
>Fix:
Unknown
>Release-Note:
>Audit-Trail:
>Unformatted:
More information about the Gcc-bugs
mailing list