String and std::string

Pavel Tsekov ptsekov@gmx.net
Fri Mar 21 12:46:00 GMT 2003


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: 
> 
> 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).

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 .




More information about the Cygwin-apps mailing list