http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52938

             Bug #: 52938
           Summary: std::string::reserve request is not maintained if
                    object is used in other object copy ctore
    Classification: Unclassified
           Product: gcc
           Version: 4.6.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: abdul.toh...@emc.com


In the line below I use 2 std::string (x and y) and I call x.reserve.  But I
found out that if I create string y from x (using copy ctor), the reserved
capacity is transferred to y and it is no longer associated with x.  


std::string x;
x.reserve(20);     /// x capacity is 20
std::cout<<x.capacity()<<' '<<(void*)(x.c_str())<<"\n";  
std::string y(x);
x="aaa";    /// x capacity should = 20 
std::cout<<x.capacity()<<' '<<(void*)(x.c_str())<<"\n";
x += "bbb";   /// x capacity should = 20 
std::cout<<x.capacity()<<' '<<(void*)(x.c_str())<<"\n";

compile and run:
20 0xf40028                                                                     
3 0xf40068                                                                      
6 0xf40098   

Notice how the pointer to string buffer is being reallocated (it shouldn't
since I have asked for x.reserve(20).

The code will behave correctly if I remove the line std::string y(x).

BTW, MS Visual c++ handles this correctly.

Reply via email to