/* This code segfaults when executed on gcc 4.01 on i586/linux (mandriva 2006) Works fine on gcc 3.x - couldn't find a newer version to test it on. PS I couldn't find anything in your bug writing guidelines what to put in "host triplet" etc... gcc -v: Using built-in specs. Target: i586-mandriva-linux-gnu Configured with: ../configure --prefix=/usr --libexecdir=/usr/lib --with-slibdir=/lib --mandir=/usr$ Thread model: posix gcc version 4.0.1 (4.0.1-5mdk for Mandriva Linux release 2006.0) */ typedef unsigned int uint; char* dummy; struct Array { char** ptr; void push(char* i) { *(ptr+alloc()) = i; } uint alloc() { ptr = &dummy; return 0; } }; int main() { Array test; char* arg = "fred"; test.push(arg); // can you please check check *test.ptr == arg, as that was what led // me to finding the bug wasn't }
No compile options just "g++ test.cpp" generates faulty code
ptr+alloc() The C and the C++ standard does not say which of ptr and alloc() is evaluated first so GCC is producing code which evaluates ptr before calling alloc which is ok for C and C++. *** This bug has been marked as a duplicate of 11751 ***