Bug 55063

Summary: g++ 3.0.1 fails to allow access to member field address
Product: [Retired] Red Hat Linux Reporter: Calvin Austin <calvin.austin>
Component: gcc3Assignee: Jakub Jelinek <jakub>
Status: CLOSED CURRENTRELEASE QA Contact:
Severity: medium Docs Contact:
Priority: medium    
Version: 7.2   
Target Milestone: ---   
Target Release: ---   
Hardware: i686   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2002-12-15 17:59:43 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Calvin Austin 2001-10-25 00:36:46 UTC
From Bugzilla Helper:
User-Agent: Mozilla/4.77 [en] (X11; U; SunOS 5.8 sun4u)

Description of problem:
Previously valid C++ code for Windows Visual C++ and
                 Solaris Forte workshop is now failing with gcc 3.0.1
                 worked on previous 2.x trains

                 The change occured during a "function rename"

                
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/decl.c.diff?r1=1.523&r2=1.524&f=h

                 in particular this line

                 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) |=
TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P
                 (basetype);

Version-Release number of selected component (if applicable):


How reproducible:
Always

Steps to Reproduce:
1.gcc3 test.cpp
2.
3.
	

Additional info:

#include <stdio.h>
#include <malloc.h>
 
#ifndef NULL
#define NULL 0L
#endif

 

class threadshadow {
public:
    int data;
};

class OSThread {
 public:
 OSThread(int width);
 OSThread(int width, int type);
 virtual void print_raw(const char* c);
};
 void OSThread::print_raw(const char* c) {
}

class Thread : public threadshadow {
public:
   Thread();
   virtual void print_raw(const char* c);
protected:
   OSThread* x2;

};

Thread::Thread() {
    x2=NULL;
}

void Thread::print_raw(const char* c) {
}
 
class JavaThread : public Thread {
public:
     JavaThread();
     int calc() {
        printf("offset=%x\n", int((int)(long)&(((JavaThread*)NULL)->x2)));
        return 0;
     }
};
 
JavaThread::JavaThread() : Thread() {
}

main ()
{
    JavaThread* t=new JavaThread();
    t->calc();
}

Comment 1 Jason Merrill 2002-02-01 17:34:26 UTC
The C++ standard specifies that offsetof may only be used for non-POD classes.  
Now, since your testcase doesn't actually use the offsetof macro, just its 
common implementation, it's merely dubious, rather than clearly ill-formed.  
I'll change g++ 3.1 to accept this code with a warning, rather than an error.

In any case, you shouldn't be writing code like this.  If you really need the 
offset, you can use ((char *)&x2 - (char *)this), but for most uses you should 
be using a pointer-to-member instead.

Jason


Comment 2 Alan Cox 2002-12-15 17:59:43 UTC
Confirmed ok in 3.2

x1.C: In member function `int JavaThread::calc()':
x1.C:44: warning: invalid offsetof from non-POD type `class Thread'; use
   pointer to member instead