[lug] C++ libraries in Linux
Scott A. Herod
herod at interact-tv.com
Tue Sep 18 11:25:03 MDT 2001
I knew that I shouldn't post any source code and show off how badly
I write. :-(
Looking at the gcc3 headers, it doesn't appear that they've yet
incorporated
stringstream.
The destructor for the strstream class is defined as so:
strstream::~strstream() {}
In the strstreambuf destructor it appears that the char* buffer is only
freed if the stream is not frozen:
strstreambuf::~strstreambuf()
{
if (_M_dynamic && !_M_frozen)
_M_free(eback()); // eback() apparently returns a pointer to the
// front of the buffer
}
So, I think now that my code should be:
> #include <strstream>
> string paraNode::toString()
> {
> string rtnval;
> strstream buf;
>
> buf << "Paragraph: "
> << ATTR_ALIGN << " = " << _align << ", "
> << ATTR_WRAP_MODE << " = " << _mode
> << ends;
> rtnval = buf.str();
buf.freeze( false );
> return rtnval;
> }
Keeping in the original thread; no comments on this subject at
dinkumware.
http://www.dinkumware.com/htm_cpl/strstrea.html
Scott
Keith Brown wrote:
>
> I hate to reply to my own mail, but my example is not correct. The buf should be
> unfrozen with buf.freeze(false), at least according to Josuttis. Additionally,
> strstream has been depreciated and he suggest you use stringstream.
>
> Keith
>
> Keith Brown wrote:
>
> > 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;
> > > }
> > >
More information about the LUG
mailing list