This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: c++/1833: inlining sometimes causes incorrect behavior


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



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]