ostrstream and memory leak

T

tcl

Questions on ostrstream.


#1) do I have a memory leak as the control exits the scope
{
ostrstream os;
os << "hello world" << endl << ends;
}


#2) if there's no memory leak in #1, is there a leak here:
{
ostrstream os;
os << "hello world" << endl << ends;
cout << os.str();
}


#3) if there's a memory leak in either #1 or #2, would adding
either of the following statements right before exiting
the scope fix it? if not, how to fix it?
os.freeze(0);
delete [] os;

Thanks,
tcl
 
J

John Harrison

tcl said:
Questions on ostrstream.


#1) do I have a memory leak as the control exits the scope
{
ostrstream os;
os << "hello world" << endl << ends;
}
No.


#2) if there's no memory leak in #1, is there a leak here:
{
ostrstream os;
os << "hello world" << endl << ends;
cout << os.str();
}

Unfortunately yes because ostrstream is very badly designed.
#3) if there's a memory leak in either #1 or #2, would adding
either of the following statements right before exiting
the scope fix it? if not, how to fix it?
os.freeze(0);

I think the above is OK but consult your documentation.
delete [] os;

The above is an error, probably shouldn't even compile.

Better still use ostringstream, then you don't need to worry about any
of this nonsense.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top