This is the mail archive of the mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: String and std::string

On Fri, 21 Mar 2003, 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())
> > IIRC.
> 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: 
>   648 alex      14   0   556  556   416 R    56.2  0.2
> 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).

There is no need to prove that - it doesn't leak according to the 
standard. Of course there may have been some faulty implementations 
that actually leak.

>From the "C++ Programming Language" by Bjarne Stroustrup:

"The data() function writes the characters of the string into an array
and returns a pointer to that array. The array is owned by the string, and
the user should not try to delete it."

c_str() is similiar .

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]