I posted below the code to duplicate this bug. I am performing a "i++" inside a std::string.insert(...) function call and it appears that the "i" value is not incrementing. This has been tested on multiple versions of GCC (2.8.1, 3.3.3, 3.3.4) running on Linux, BSD, and Windows (via Cygwin). The code does work properly in Visual Studio .NET. I tried different -O optimizations, including turning them off, and the problem persisted. Compiled this code by typing: g++ -Wall -o main ./main.cpp Haven't reported a bug before, so I don't know what the "host triplet, target triplet, and build triplet" things are. Let me know if you need any more information. Hope this helps. ---------------------------------------------------------------------------- $ gcc -v Reading specs from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.3/specs Configured with: /var/tmp/portage/gcc-3.3.3-r6/work/gcc-3.3.3/configure --prefix=/usr --bindir=/usr/i686-pc-linux-gnu/gcc-bin/3.3 --includedir=/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.3/include --datadir=/usr/share/gcc-data/i686-pc-linux-gnu/3.3 --mandir=/usr/share/gcc-data/i686-pc-linux-gnu/3.3/man --infodir=/usr/share/gcc-data/i686-pc-linux-gnu/3.3/info --enable-shared --host=i686-pc-linux-gnu --target=i686-pc-linux-gnu --with-system-zlib --enable-languages=c,c++ --enable-threads=posix --enable-long-long --disable-checking --disable-libunwind-exceptions --enable-cstdio=stdio --enable-version-specific-runtime-libs --with-gxx-include-dir=/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.3/include/g++-v3 --with-local-prefix=/usr/local --enable-shared --enable-nls --without-included-gettext --disable-multilib --enable-__cxa_atexit --enable-clocale=generic Thread model: posix gcc version 3.3.3 20040412 (Gentoo Linux 3.3.3-r6, ssp-3.3.2-2, pie-8.7.6) ---------------------------------------------------------------------------- SOURCE CODE ---------------------------------------------------------------- ---------------------------------------------------------------------------- #include <iostream> #include <string> using namespace std; int main(void) { string array1; string array2; int i; array1 = "Test String #1"; array2 = ""; cout << "[" << array1 << "][" << array2 << "]" << endl; i = 0; while(i < (int)strlen(array1.c_str())) { // array2.insert(i, &array1[i], 1); ++i; // <-- This line works array2.insert(i, &array1[i++], 1); // <-- This line doesn't work } cout << "[" << array1 << "][" << array2 << "]" << endl; return 0; }
The code you posted is undefined, you are modifing i and also getting the value of i without a sequence point inbetween them. *** This bug has been marked as a duplicate of 11751 ***