Utilizing the post increment operator within a stream operation does not result in the expected output. Output is reversed. Based on the grammer of the language, this should work as expected and not as currently implemented. THe following program will replicate the problem: #include <iostream> #include <fstream> using namespace std; char *str = "0123456789ABCDEF"; int main(int argc, char **argv) { char *ptr; #if defined(COUT_TOO) ptr = str; cout << *ptr++ << *ptr++ << ' '; cout << *ptr++ << *ptr++ << ' '; cout << *ptr++ << *ptr++ << ' '; cout << *ptr++ << *ptr++ << ' '; cout << *ptr++ << *ptr++ << ' '; cout << *ptr++ << *ptr++ << ' '; cout << *ptr++ << *ptr++ << ' '; cout << *ptr++ << *ptr++ << ' '; cout << endl; #endif ostream *outStream; outStream = new ofstream("foo.txt"); ptr = str; *outStream << *ptr++ << *ptr++ << ' '; *outStream << *ptr++ << *ptr++ << ' '; *outStream << *ptr++ << *ptr++ << ' '; *outStream << *ptr++ << *ptr++ << ' '; *outStream << *ptr++ << *ptr++ << ' '; *outStream << *ptr++ << *ptr++ << ' '; *outStream << *ptr++ << *ptr++ << ' '; *outStream << *ptr++ << *ptr++ << ' '; *outStream << endl; delete outStream; // Same concept, different syntax works. #if defined(COUT_TOO) ptr = str; cout << *ptr++; cout << *ptr++ << ' '; cout << *ptr++; cout << *ptr++ << ' '; cout << *ptr++; cout << *ptr++ << ' '; cout << *ptr++; cout << *ptr++ << ' '; cout << *ptr++; cout << *ptr++ << ' '; cout << *ptr++; cout << *ptr++ << ' '; cout << *ptr++; cout << *ptr++ << ' '; cout << *ptr++; cout << *ptr++ << ' '; cout << endl; #endif outStream = new ofstream("bar.txt"); ptr = str; *outStream << *ptr++; *outStream << *ptr++ << ' '; *outStream << *ptr++; *outStream << *ptr++ << ' '; *outStream << *ptr++; *outStream << *ptr++ << ' '; *outStream << *ptr++; *outStream << *ptr++ << ' '; *outStream << *ptr++; *outStream << *ptr++ << ' '; *outStream << *ptr++; *outStream << *ptr++ << ' '; *outStream << *ptr++; *outStream << *ptr++ << ' '; *outStream << *ptr++; *outStream << *ptr++ << ' '; *outStream << endl; } The above program was compiled with the following command: g++ -O0 -o foobar test.cxx -DCOUT_TOO The output on the screen and found in the files is thus: 10 32 54 76 98 BA DC FE 01 23 45 67 89 AB CD EF The specs of the compiler obtained with g++ -v is: Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.3.3/specs Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --disable-libunwind-exceptions --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux Thread model: posix gcc version 3.3.3 20040216 (Red Hat Linux 3.3.3-2) I have even tried the latest snapshot of GCC 3.3.3 and it fails as well. I also compiled 3.3.1 and used it with the same results.
No bug, please read the page on frequently reported bugs. operator<< is not a sequence point, so the question of when the pointer is increased and when it is dereferenced is undefined. W.
(In reply to comment #1) > No bug, please read the page on frequently reported bugs. operator<< > is not a sequence point, so the question of when the pointer is > increased and when it is dereferenced is undefined. > > W. It's interesting that this has worked historically. I see g++ 2.9.5 work appropriately on our Sun Solaris stations, as well as V3 work appropriately on our Macintosh systems. I guess we'll resort to using something that actually works as most people would expect it to.
The standard says that the behavior of your program is undefined in this case. Newer compilers are better at optimizing code, so they trip users writing undefined code more often. It's not the compiler's fault if it reorders code that the standard allows it to reorder. W. PS: To understand what's going on, read the entry on -Wsequence-point in the manual.
Reopen these bugs...
...mark as duplicate of PR 11751. *** This bug has been marked as a duplicate of 11751 ***