Balogh, Ray wrote:
Dear GCC folks:I'm having a problem with GCC 2.95.3 that appears to be a compiler bug. It seems to be optimizing out inlined function code with side effects, and is related to binding a non-const pointer to a const pointer reference function parameter. The problem only happens with optimization on, and goes away with -O0. inline unsigned unpack2 (const byte *&data) { unsigned val = data[0] << 8 | data[1]; data += 2; return val; } inline unsigned unpack2 (const byte *&data, unsigned &count) { unsigned val = data[0] << 8 | data[1]; data += 2; count -= 2; return val; } void extractInfo (byte *&data, unsigned &datalen, unsigned &f1, unsigned &f2) { cout << "data = " << data << ", datalen = " << datalen << endl; f1 = unpack2 (data, datalen); cout << "data = " << data << ", datalen = " << datalen << endl; byte *peek = data; //const byte *peek = data; // **** Adding "const" works around the compiler problem **** // Another work-around is to uncomment the non-const f1 of unpack2() above unsigned tmp = datalen; cout << "peek = " << peek << ", tmp = " << tmp << endl; (void) unpack2 (peek, tmp); // skip over irrelevant field cout << "peek = " << peek << ", tmp = " << tmp << endl; (void) unpack2 (peek, tmp); // skip over irrelevant field cout << "peek = " << peek << ", tmp = " << tmp << endl; unsigned xlen = unpack2 (peek, tmp); cout << "peek = " << peek << ", tmp = " << tmp << endl; f2 = xlen; }
In my opinion, the bug is that this even compiles. It's invalid to write char *pc; const char **ppc = &pc; Why should it be valid to write char *pc; const char *&rpc = pc; ? Sebastian
