Bug 17639 - ++ increment not working in std::string.insert
Summary: ++ increment not working in std::string.insert
Status: RESOLVED DUPLICATE of bug 11751
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.3.4
: P3 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2004-09-23 20:37 UTC by Ryan Barnard
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description Ryan Barnard 2004-09-23 20:37:06 UTC
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
--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
--with-local-prefix=/usr/local --enable-shared --enable-nls
--without-included-gettext --disable-multilib --enable-__cxa_atexit
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;
Comment 1 Andrew Pinski 2004-09-23 20:54:48 UTC
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 ***