[lug] C++ libraries in Linux
D. Stimits
stimits at idcomm.com
Tue Sep 18 11:58:47 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? 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.
std::string creates an object that has a destructor, and it cleans up
after itself. When assigning char* type strings to std::string, it
copies by value. The leak is where the strstream is frozen, via the
.str() member function. You can then either manually delete the memory,
or you can unfreeze (non-frozen means it will clean up after itself via
destructor):
/////////////////
#include <string>
#include <strstream>
std::string paragraph( int whole, float deci )
{
strstream buf;
buf << "Paragraph: "
<< whole
<< ", " << deci
<< ends;
std::string rtnval = buf.str();
buf.freeze(0); // unfreeze.
return rtnval;
}
int main()
{
cout << paragraph( 2, 3.14 ) << endl;
return 0;
}
/////////////////////////
>
> 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
More information about the LUG
mailing list