[lug] C++ libraries in Linux

Keith Brown kbrown8 at uswest.net
Tue Sep 18 11:15:13 MDT 2001


Jeff Walker wrote:

> Perhaps this is a place where C++ differs from C (I am still learning some
> of those), but if you free (delete) the string, it seems like a pointer to
> that string would be invalid.  Am I wrong?

No, s is deleted after assigning it to rtnval, which makes a copy of s.
Actually, the following code is more correct.
rtnval = buf.str();
buf.freeze(false);  // buf takes ownership back
return rtnval;

In the standard, strstream has been depreicated and replaced by stringstream,
which returns a C++ string from the str() method.   Avoiding ths nonsense.


> If you copy the string, you will
> have the same problem as the original code, you have a lasting string that
> never gets free'd.
>
> Or, you could avoid the whole mess and say that this behaves like strdup(),
> where, if you use it, it allocates memory that you must free yourself.  Of
> course, I know what the "C founding fathers" would have to say about that (I
> forget who said that if you allocate memory, you should free it yourself,
> not relying of the caller to free it for you), but sometimes, you have to do
> such things. ;-)
>
> --
> Jeff Walker                   MatchLogic, Inc.
> jwalker at matchlogic.com        7233 Church Ranch Blvd.
> Voice 1 (303) 222-2105        Westminster, CO  80021
> Fax   1 (303) 222-2001        www.matchlogic.com
>
> -----Original Message-----
> From: Keith Brown [mailto:kbrown8 at uswest.net]
> Sent: Tuesday, September 18, 2001 10:10 AM
> To: lug at lug.boulder.co.us
> Subject: Re: [lug] C++ libraries in Linux
>
> Hi,
>
> I believe you have memory leak in your example.  Once you do buf.str(), the
> program owns the *char that is returned, unless it tells strstream
> otherwise,
> which I forget how to do.  I believe you need to do it like:
>
> char * s = buf.str();
> rtnval = s;
> delete [] s;
> return rtnval;
>
> Keith
>
> "Scott A. Herod" wrote:
>
> > Hi,
> >   I use something like the following:
> >
> > #include
> > <strstream>
> > string paraNode::toString()
> > {
> >     string rtnval;
> >     strstream buf;
> >
> >     buf << "Paragraph: "
> >         << ATTR_ALIGN << " = " << _align << ", "
> >         << ATTR_WRAP_MODE << " = " << _mode
> >         << ends;
> >
> >     rtnval = buf.str();
> >     return rtnval;
> > }
> >
> > > Can it be true?  There are no converters to go from an int to a string?
> Or
> > > a double to a string?
> > >
> > > --Keith
> > _______________________________________________
> > Web Page:  http://lug.boulder.co.us
> > Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
>
> --
> Keith Brown
> 12640 Xavier St
> Broomfield, CO 80020
> 303.438.8169
>
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug

--
Keith Brown
12640 Xavier St
Broomfield, CO 80020
303.438.8169





More information about the LUG mailing list