c++/1833: inlining sometimes causes incorrect behavior

Wolfgang Bangerth bangerth@ticam.utexas.edu
Wed Oct 30 17:26:00 GMT 2002


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

From: Wolfgang Bangerth <bangerth@ticam.utexas.edu>
To: gcc-bugs@gcc.gnu.org, <gcc-gnats@gcc.gnu.org>
Cc:  
Subject: Re: c++/1833: inlining sometimes causes incorrect behavior
Date: Wed, 30 Oct 2002 19:27:24 -0600 (CST)

 I can confirm this also with newer gcc's, and this indeed very surprising. 
 Here's the thing:
 -----------------------------------
 #include <iostream>
 using namespace std;
 
 typedef char *pchar;
 
 void advance_by_3 (pchar &s) { s += 3; }
 
 inline void frob_fail (char *s) {
   advance_by_3((pchar)s);
   cerr << "should be 34567: " << s << endl;
 }
 
 void frob_pass (char *s) {
   advance_by_3((pchar)s);
   cerr << "should be 34567: " << s << endl;
 }
 
 int main () {
   char *x = "01234567";  frob_fail(x);
   char *y = "01234567";  frob_pass(y);
 }
 -----------------------------------
 and I get:
   tmp/g> /home/bangerth/bin/gcc-3.2.1-pre/bin/c++ -O2 x.cc
   tmp/g> ./a.out
   should be 34567: 01234567
   should be 34567: 01234567
 
   tmp/g> /home/bangerth/bin/gcc-3.2.1-pre/bin/c++ x.cc
   tmp/g> ./a.out
   should be 34567: 34567
   should be 34567: 34567
 
 With present CVS I get the first results also for -O0. For 2.95, I get the 
 wrong result only for the _inlined_ function, where I now get it 
 uniformly.
 
 The problem is rather surreal, since touching it somewhere makes it go 
 away. For example, removing the (pchar) casts in the call to advance_by_3 
 make a difference, although the variable is already a char*.
 
 Regards
   Wolfgang
 
 -------------------------------------------------------------------------
 Wolfgang Bangerth              email:           bangerth@ticam.utexas.edu
                                www: http://www.ticam.utexas.edu/~bangerth
 
 



More information about the Gcc-prs mailing list