public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* Re: c++/9377: g++ 64bit calls wrong function -> Multi-inheritance: pointer to member function of the 2nd base calss points to wrong place
@ 2003-01-21 21:36 Wolfgang Bangerth
  0 siblings, 0 replies; 9+ messages in thread
From: Wolfgang Bangerth @ 2003-01-21 21:36 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c++/9377; it has been noted by GNATS.

From: Wolfgang Bangerth <bangerth@ticam.utexas.edu>
To: Andrew Xiong <axiong@ca.ibm.com>
Cc: bangerth@dealii.org, <gcc-bugs@gcc.gnu.org>, <gcc-gnats@gcc.gnu.org>
Subject: Re: c++/9377: g++ 64bit calls wrong function -> Multi-inheritance:
 pointer to member function of the 2nd base calss points to wrong place
Date: Tue, 21 Jan 2003 15:30:10 -0600 (CST)

 Andrew,
 Thanks for the answers.
 
 >         d1ptr = (D*) new B2 ;  //with the cast, we can also do this
 >         printf( "%s\n", (d1ptr->*select(1))() ) ;
 
 This can really only work by chance. By creating a B2 you run code that 
 sets up the virtual function table for a B2, and invoking a function from 
 the D function table will invoke undefined behavior. I also bet that this 
 will not work with virtual inheritance.
 
 W.
 
 -------------------------------------------------------------------------
 Wolfgang Bangerth             email:            bangerth@ticam.utexas.edu
                               www: http://www.ticam.utexas.edu/~bangerth/
 
 
 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: c++/9377: g++ 64bit calls wrong function -> Multi-inheritance: pointer to member function of the 2nd base calss points to wrong place
@ 2003-01-21 22:26 axiong
  0 siblings, 0 replies; 9+ messages in thread
From: axiong @ 2003-01-21 22:26 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c++/9377; it has been noted by GNATS.

From: axiong@ca.ibm.com
To: Wolfgang Bangerth <bangerth@ticam.utexas.edu>
Cc: bangerth@dealii.org, gcc-bugs@gcc.gnu.org, gcc-gnats@gcc.gnu.org
Subject: Re: c++/9377: g++ 64bit calls wrong function -> Multi-inheritance: pointer
 to member function of the 2nd base calss points to wrong place
Date: Tue, 21 Jan 2003 17:20:29 -0500

 Hi, Wolfgang,
 
 Thanks. For the code:
 >         d1ptr = (D*) new B2 ;  //with the cast, we can also do this
 >         printf( "%s\n", (d1ptr->*select(1))() ) ;
 The expected output from the "printf" is suppose to be:
 ---------------------------------------
 B2::bar() is called
 
 More information: both 32-bit g++ and 64-bit g++ (Linuc PPC) give correct
 output for it, and also
 xlC on AIX gives correct output for it (B2::bar() is called).
 
 Regards,
 
 Andrew
 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: c++/9377: g++ 64bit calls wrong function -> Multi-inheritance: pointer to member function of the 2nd base calss points to wrong place
@ 2003-01-21 20:56 axiong
  0 siblings, 0 replies; 9+ messages in thread
From: axiong @ 2003-01-21 20:56 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c++/9377; it has been noted by GNATS.

From: axiong@ca.ibm.com
To: bangerth@dealii.org, "Andrew Xiong" <axiong@ca.ibm.com>,
   gcc-bugs@gcc.gnu.org, gcc-prs@gcc.gnu.org, nobody@gcc.gnu.org,
   gcc-gnats@gcc.gnu.org
Cc:  
Subject: Re: c++/9377: g++ 64bit calls wrong function -> Multi-inheritance: pointer
 to member function of the 2nd base calss points to wrong place
Date: Tue, 21 Jan 2003 15:49:59 -0500

 Answers to the following questions:
 > My questions are (maybe you can send an answer to these):
 > - why do you need the cast from &B2::bar to D::*? I
 >   think that's the crucial step -- if your program is
 >   supposed to work, then this cast would not be necessary,
 >   but rather be an implicit default conversion.
 > - what's the purpose of the select() method you have there?
 >   The comment implies that without it, there's a problem.
 >   But what exactly is it?
 
 (1) The "select() method" was put there for another 64-bit
     C++ compiler, not for g++ 64-bit compiler. It does not
     influence 64-bit g++ compiling (I forgot to delete it when
     I was submitting the gnat report). it can be deleted or
     kept there.
 
 (2) The C++ code was reduced from a C++ program written to
     test the pointer to member function and the virtual function
     table. The cast may be useful - it will direct the pointer
     to where the programmer wants, and it may be helpful for
     function returning when multi-inheritance occurs,
     for example, a C++ programmer may write:
 /*************************************************************/
 // put the classes B1, B2, and D here
 
 typedef char * (D::*PMF)() ;
 
 PMF select(int flag) {
         if (flag==0) return &B1::foo ; //the cast occurs here
         else return &B2::bar ;
 }
 
 main() {
         int codition = 1;
 
         D *d1ptr = new D ;     //another kind of polymorphism?
         printf( "%s\n", (d1ptr->*select(condition)() ) ;
 
         d1ptr = (D*) new B2 ;  //with the cast, we can also do this
         printf( "%s\n", (d1ptr->*select(1))() ) ;
 
         return 0 ;
 }
 /*************************************************************/
 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: c++/9377: g++ 64bit calls wrong function -> Multi-inheritance: pointer to member function of the 2nd base calss points to wrong place
@ 2003-01-21 20:06 Janis Johnson
  0 siblings, 0 replies; 9+ messages in thread
From: Janis Johnson @ 2003-01-21 20:06 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c++/9377; it has been noted by GNATS.

From: Janis Johnson <janis187@us.ibm.com>
To: axiong@ca.ibm.com
Cc: gcc-gnats@gcc.gnu.org, bangerth@ticam.utexas.edu
Subject: Re: c++/9377: g++ 64bit calls wrong function -> Multi-inheritance: pointer to member function of the 2nd base calss points to wrong place
Date: Tue, 21 Jan 2003 11:59:40 -0800

 On Mon, Jan 20, 2003 at 07:35:07PM -0000, axiong@ca.ibm.com wrote:
 > 
 > => For the given C++ code, if we compile it with 646-bit
 >    g++ compiler (/opt/cross/bin/powerpc64-linux-g++) and 
 >    then run it, we have output:
 >    --------------------
 >    B1::foo() is called
 > 
 >    The correct output sould be:
 >    --------------------
 >    D::bar() is called
 
 I can confirm that the 3.2 cross compiler gives this output.  Please try
 building a 3.2.1 compiler and trying that; I get the results you expect
 with 64-bit native compilers (powerpc64-linux) with 3.2.1, the 3.2-branch,
 3.3-branch, and mainline.
 
 For some reason I can't run any of my cross compilers right now, and I've
 got a flu-like thing that makes thinking difficult.
 
 Janis


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: c++/9377: g++ 64bit calls wrong function -> Multi-inheritance: pointer to member function of the 2nd base calss points to wrong place
@ 2003-01-21 17:36 Wolfgang Bangerth
  0 siblings, 0 replies; 9+ messages in thread
From: Wolfgang Bangerth @ 2003-01-21 17:36 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c++/9377; it has been noted by GNATS.

From: Wolfgang Bangerth <bangerth@ticam.utexas.edu>
To: Andrew Xiong <axiong@ca.ibm.com>
Cc: gcc-bugs@gcc.gnu.org, <gcc-gnats@gcc.gnu.org>
Subject: Re: c++/9377: g++ 64bit calls wrong function -> Multi-inheritance:
 pointer to member function of the 2nd base calss points to wrong place
Date: Tue, 21 Jan 2003 11:30:30 -0600 (CST)

 > Actually, with the C++ compiler (xlC) on AIX, for both 32-bit and 64-bit
 > compiling,  we can do:
 >    PMF aPMF = (char *(D::*)()) &B2::bar ;
 > And also,  with g++ for 32-bit compiling, we can do:
 >    PMF aPMF = (char *(D::*)()) &B2::bar ;
 > 
 > The problem is only for g++ 64-bit compiling: we can not do it only for g++
 > 64-bit compiling.
 
 The fact that it works with one compiler and not with the other is of 
 course no proof of either one being right :-) What I meant is that the 
 question 32 vs 64 bit affects the layout of the virtual function table, so 
 I wouldn't be surprised if an accidentially working program breaks when 
 you change something like the layout of the vt.
 
 That being said, I reopened the report to let others have a second look on 
 things.
 
 Regards
   Wolfgang
 
 -------------------------------------------------------------------------
 Wolfgang Bangerth             email:            bangerth@ticam.utexas.edu
                               www: http://www.ticam.utexas.edu/~bangerth/
 
 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: c++/9377: g++ 64bit calls wrong function -> Multi-inheritance: pointer to member function of the 2nd base calss points to wrong place
@ 2003-01-21 17:27 bangerth
  0 siblings, 0 replies; 9+ messages in thread
From: bangerth @ 2003-01-21 17:27 UTC (permalink / raw)
  To: axiong, gcc-bugs, gcc-prs, nobody

Synopsis: g++ 64bit calls wrong function -> Multi-inheritance: pointer to member function of the 2nd base calss points to wrong place

State-Changed-From-To: closed->open
State-Changed-By: bangerth
State-Changed-When: Tue Jan 21 17:27:05 2003
State-Changed-Why:
    I am still pretty sure that what you try is not legal, but
    I'll reopen it and let someone else have a second look on it.
    
    My questions are (maybe you can send an answer to these):
    - why do you need the cast from &B2::bar to D::*? I
      think that's the crucial step -- if your program is
      supposed to work, then this cast would not be necessary,
      but rather be an implicit default conversion.
    - what's the purpose of the select() method you have there?
      The comment implies that without it, there's a problem.
      But what exactly is it?
    
    My idea about this is that if you take &B2::bar, then since
    B2::bar is a virtual function, the member function pointer
    actually is a pointer into the virtual function table of B2.
    If you later call the function with an object of type "D",
    then you call it through the virtual function table of D,
    so a conversion is necessary. The question is whether this
    happens.
    
    Regards
      Wolfgang
    
    PS: The code works on sparc in 32 and 64 bit mode as you
    would expect it, but I can't check for the PPC platform
    you have.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=9377


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: c++/9377: g++ 64bit calls wrong function -> Multi-inheritance: pointer to member function of the 2nd base calss points to wrong place
@ 2003-01-21 17:06 axiong
  0 siblings, 0 replies; 9+ messages in thread
From: axiong @ 2003-01-21 17:06 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c++/9377; it has been noted by GNATS.

From: axiong@ca.ibm.com
To: bangerth@dealii.org, "Andrew Xiong" <axiong@ca.ibm.com>,
   gcc-bugs@gcc.gnu.org, gcc-prs@gcc.gnu.org, nobody@gcc.gnu.org,
   gcc-gnats@gcc.gnu.org
Cc:  
Subject: Re: c++/9377: g++ 64bit calls wrong function -> Multi-inheritance: pointer
 to member function of the 2nd base calss points to wrong place
Date: Tue, 21 Jan 2003 12:04:20 -0500

 Actually, with the C++ compiler (xlC) on AIX, for both 32-bit and 64-bit
 compiling,  we can do:
    PMF aPMF = (char *(D::*)()) &B2::bar ;
 And also,  with g++ for 32-bit compiling, we can do:
    PMF aPMF = (char *(D::*)()) &B2::bar ;
 
 The problem is only for g++ 64-bit compiling: we can not do it only for g++
 64-bit compiling.
 I believe that the code is correct accorging to standard C++. Please take a
 look.
 
 Regards,
 
 Andrew
 
 
 
                                                                                                                                        
                       bangerth@dealii.o                                                                                                
                       rg                       To:       "Andrew Xiong" <axiong@ca.ibm.com>, gcc-bugs@gcc.gnu.org,                     
                                                 gcc-prs@gcc.gnu.org, nobody@gcc.gnu.org                                                
                       01/21/2003 11:20         cc:                                                                                     
                       AM                       Subject:  Re: c++/9377: g++ 64bit calls wrong function -> Multi-inheritance: pointer to 
                       Please respond to         member function of the 2nd base calss points to wrong place                            
                       bangerth; Please                                                                                                 
                       respond to                                                                                                       
                       "Andrew Xiong";                                                                                                  
                       Please respond to                                                                                                
                       gcc-bugs; Please                                                                                                 
                       respond to                                                                                                       
                       gcc-prs; Please                                                                                                  
                       respond to                                                                                                       
                       nobody; Please                                                                                                   
                       respond to                                                                                                       
                       gcc-gnats                                                                                                        
                                                                                                                                        
                                                                                                                                        
 
 
 
 Synopsis: g++ 64bit calls wrong function -> Multi-inheritance: pointer to
 member function of the 2nd base calss points to wrong place
 
 State-Changed-From-To: open->closed
 State-Changed-By: bangerth
 State-Changed-When: Tue Jan 21 16:20:51 2003
 State-Changed-Why:
     You can't do this:
       PMF aPMF = (char *(D::*)()) &B2::bar ;
     If you want to call B2::bar with a pointer to a D object,
     then you will have to take its address as
       &D::bar
 
     W.
 
 
 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=9377
 
 
 
 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: c++/9377: g++ 64bit calls wrong function -> Multi-inheritance: pointer to member function of the 2nd base calss points to wrong place
@ 2003-01-21 16:20 bangerth
  0 siblings, 0 replies; 9+ messages in thread
From: bangerth @ 2003-01-21 16:20 UTC (permalink / raw)
  To: axiong, gcc-bugs, gcc-prs, nobody

Synopsis: g++ 64bit calls wrong function -> Multi-inheritance: pointer to member function of the 2nd base calss points to wrong place

State-Changed-From-To: open->closed
State-Changed-By: bangerth
State-Changed-When: Tue Jan 21 16:20:51 2003
State-Changed-Why:
    You can't do this:
      PMF aPMF = (char *(D::*)()) &B2::bar ;
    If you want to call B2::bar with a pointer to a D object,
    then you will have to take its address as
      &D::bar
    
    W.
    

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=9377


^ permalink raw reply	[flat|nested] 9+ messages in thread

* c++/9377: g++ 64bit calls wrong function -> Multi-inheritance: pointer to member function of the 2nd base calss points to wrong place
@ 2003-01-20 19:36 axiong
  0 siblings, 0 replies; 9+ messages in thread
From: axiong @ 2003-01-20 19:36 UTC (permalink / raw)
  To: gcc-gnats


>Number:         9377
>Category:       c++
>Synopsis:       g++ 64bit calls wrong function -> Multi-inheritance: pointer to member function of the 2nd base calss points to wrong place
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Jan 20 19:36:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     Guan-Zhu (Andrew) Xiong
>Release:        gcc version 3.2
>Organization:
>Environment:
Linux Version 2.4.19:
SuSE SLES 8 (ppc) - Kernel 2.4.19-ul1-ppc64-SMP (34).
>Description:
=> For 64-bit g++ compiling, for the following 
   Multi-inheritance:
      struct B1
      struct B2
      struct D: B1, B2
   pointer to member function of the 2nd base class B2
   points to a wrong place. We want to call the function
   bar() through a pointer to memebr function of B2, but
   another function foo() defined in the drived calss is
   called.

=> For the given C++ code, if we compile it with 646-bit
   g++ compiler (/opt/cross/bin/powerpc64-linux-g++) and 
   then run it, we have output:
   --------------------
   B1::foo() is called

   The correct output sould be:
   --------------------
   D::bar() is called
   
>How-To-Repeat:
/*********************************************************
 * -> compile the code with 64-bit g++ compiler
 *    - like: powerpc64-linux-g++ -o hello hello.C
 * -> run it
 *    - hello
 ********************************************************/ 

#include <stdio.h>

struct B1{
        virtual char *foo() { return "B1::foo() is called "; }
};

struct B2{
        virtual char * bar() { return "B2::bar() is called "; }
};

struct D : B1, B2 {         //change the order of B1 and B2 -> passed
        char* foo() { return "D::foo() is called "; }
        char* bar() { return "D::bar() is called "; }
};

typedef char * (D::*PMF)();

PMF select() { return 0; }  //put this function here to avoid memory fault

main() {

        D *d1ptr = new D ;

        PMF aPMF = (char *(D::*)()) &B2::bar ;

        printf( "%s\n", (d1ptr->*aPMF)() ) ;

        return 0 ;
}
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2003-01-21 22:26 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-21 21:36 c++/9377: g++ 64bit calls wrong function -> Multi-inheritance: pointer to member function of the 2nd base calss points to wrong place Wolfgang Bangerth
  -- strict thread matches above, loose matches on Subject: below --
2003-01-21 22:26 axiong
2003-01-21 20:56 axiong
2003-01-21 20:06 Janis Johnson
2003-01-21 17:36 Wolfgang Bangerth
2003-01-21 17:27 bangerth
2003-01-21 17:06 axiong
2003-01-21 16:20 bangerth
2003-01-20 19:36 axiong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).