stdcxx-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Anton Pevtsov" <>
Subject RE: basic_string::insert (iterator p, InputIterator first, InputIterator last) on Linux, gcc-4.0.2
Date Wed, 05 Apr 2006 08:08:55 GMT
I updated the test for the replace method and reproduced the problem
discovered in the insert's test. 
This test fails on Widows, MSVC 7.1, build 11d and on SuSe Linux 9.1 gcc

#include <iostream>
#include <string>

static const char* test = "babc";

int main (void)
    std::string s ("abc");
    s.replace (s.begin (), s.begin (), s.begin () + 1, s.begin () + 2);

    std::cout << "Expected " << test << " and got " << s <<

    return 0;

The output is "Expected babc and got aabc".

I investigated the problem and found that cause is in the
__rw_replace_aux function implementation in, line 566.
There is the following code (line 643):

// Current reference has enough room.
        if (__rem)  
        for (__d = 0; __d < (size_type)__n2; __d++)
            traits_type::assign (*(__s._C_data+__pos+__d), *__first2++);
        traits_type::assign (__s._C_data[__s._C_pref()->_C_size._C_size 
                                         = __len], value_type());

For the exmaple above we have __pos = 0, __n2 = 1, __n = 0, __rem = 3,
and __s._C_data points to the begin of the "abc" string.
Note, that the iterator __first2 points to the same string, to symbol

As first step this code moves the string and this operation results in
But the __first2 doesn't change - and now it points to the same place,
but there is the symbol 'a' !

Second step just copies the second 'a' to first 'a' in the string and
third step places zero at the end of string.
So, the result of the replace call differs from the expected one.

Anton Pevtsov

-----Original Message-----
From: Martin Sebor [] 
Sent: Tuesday, April 04, 2006 16:22
Subject: Re: basic_string::insert (iterator p, InputIterator first,
InputIterator last) on Linux, gcc-4.0.2

Anton Pevtsov wrote:

> It works fine on MSVC. The test fails on SuSe Linux 9.1 with gcc 4.0.2

> only. And it looks like this is a problem on my local machine - Liviu 
> couldn't reproduce it. When I finish the replace test I'll clean up 
> all on my Linux machine and build the library from scratch again.

It might also be a problem with the compiler. I don't think I'll be able
to look into it until I get back. Working remotely from where I am
(Berlin, Germany) is excruciatingly slow and I cannot reproduce it on my
laptop. Try stepping through the code in the debugger to see if it
behaves correctly.


View raw message