public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/52938] New: std::string::reserve request is not maintained if object is used in other object copy ctore
@ 2012-04-11 20:23 abdul.tohmaz at emc dot com
  2012-04-11 20:26 ` [Bug libstdc++/52938] " pinskia at gcc dot gnu.org
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: abdul.tohmaz at emc dot com @ 2012-04-11 20:23 UTC (permalink / raw)
  To: gcc-bugs

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: unassigned@gcc.gnu.org
        ReportedBy: abdul.tohmaz@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.


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2012-04-13 13:38 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-11 20:23 [Bug c++/52938] New: std::string::reserve request is not maintained if object is used in other object copy ctore abdul.tohmaz at emc dot com
2012-04-11 20:26 ` [Bug libstdc++/52938] " pinskia at gcc dot gnu.org
2012-04-11 20:32 ` abdul.tohmaz at emc dot com
2012-04-11 20:57 ` redi at gcc dot gnu.org
2012-04-11 21:08 ` redi at gcc dot gnu.org
2012-04-11 22:34 ` [Bug libstdc++/52938] std::string::reserve request is not maintained if object is used in other object copy ctor paolo.carlini at oracle dot com
2012-04-11 23:50 ` abdul.tohmaz at emc dot com
2012-04-11 23:53 ` abdul.tohmaz at emc dot com
2012-04-11 23:57 ` paolo.carlini at oracle dot com
2012-04-11 23:58 ` paolo.carlini at oracle dot com
2012-04-12  0:12 ` abdul.tohmaz at emc dot com
2012-04-12  0:22 ` redi at gcc dot gnu.org
2012-04-12 23:07 ` abdul.tohmaz at emc dot com
2012-04-13  8:31 ` redi at gcc dot gnu.org
2012-04-13 13:38 ` abdul.tohmaz at emc dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).