This is the mail archive of the
mailing list for the Cygwin project.
RE: String and std::string
- From: "Morrison, John" <John dot Morrison at uk dot experian dot com>
- To: "'cygwin-apps at cygwin dot com'" <cygwin-apps at cygwin dot com>
- Date: Fri, 21 Mar 2003 13:01:29 -0000
- Subject: RE: String and std::string
Robert Collins wrote:
> On Fri, 2003-03-21 at 23:12, Alex Tibbles wrote:
>>> I don't care whether you use std::string, or
>>> String++ for any new code,
>>> as long as:
>>> 1) You don't leak memory (std::string will leak if
>>> you use c_str())
>> I've been unable to confirm this. I tried the attached
>> program (compiled with gcc -lstdc++ stringleak.cpp)
>> and got the following results from top:
> Well, your test is flawed for the behaviour I was referring to. A new
> one is below.
>> PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM
>> 648 alex 14 0 556 556 416 R 56.2 0.2
>> TIME COMMAND
>> 3:02 a.out
>> Am I correct in concluding that std::string::c_str()
>> does not leak? I'm interested as I use c_str() a fair
>> amount for interfacing C++ with C (I believe that was
>> what it was designed for, but I'm only guessing).
>> #include <string>
>> #include <iostream>
>> int main()
>> std::string bleg("bleg");
>> while (true)
>> std::cout << bleg.c_str();
>> return 0;
> the above won't leak, because c_str() returns the internal buffer -
> which isn't being altered.
> this may well. Look in the g++ library for freeze() (again, IIRC). The
> thing I saw when I was reading the source, was that the internal
> buffer was not delete'd once it was handed out via c_str(). That
> was with the libstc++ for gcc 2.95 as well. I've just checked the g++
> 3 implementation, and it's different to what I recall from before...
> #include <string>
> #include <iostream>
> int main ()
> while (true)
> std::string foo("bar");
> char const *leakingpointer = foo.c_str();
> return 0;
I thought you were supposed to call release() on any c_str() calls...
of cause, it's been two or three years since I played with this so,
I might be mistaken...
Information in this email and any attachments are confidential, and may
not be copied or used by anyone other than the addressee, nor disclosed
to any third party without our permission. There is no intention to
create any legally binding contract or other commitment through the use
of this email.
Experian Limited (registration number 653331).
Registered office: Talbot House, Talbot Street, Nottingham NG1 5HF