Memory deallocation issues with ostringstream

  • Thread starter Generic Usenet Account
  • Start date
G

Generic Usenet Account

With the deprecated ostrstream class, when the constructor is invoked
without arguments, memory is dynamically allocated. In that case the
onus on freeing the memory lies with the user. Typically this is done
by obtaining the char buffer (by invoking the str() method) and then
explicitly deleting it.

Does the ostringstream class also have the same issue? I mean, if I
instantiate ostringstream without any constructor arguments, is the
onus of freeing the dynamically allocated memory still on me?

If my question is not clear, I hope the following code snippet will
help. I need the answer to the question
// ******* Do we need to do something similar for
ostringstream? *******

in the sample code.

Thanks,
Song

///////////////////////////////////////////////////////////////////
#include <iostream>
#include <ctime>

#ifdef DEPRECATED
#include <strstream>
#else
#include <sstream>
#endif // DEPRECATED

using namespace std;

main()
{
#ifdef DEPRECATED
ostrstream os;
#else
ostringstream os;
#endif // DEPRECATED

time_t now;
time(&now);
os << "Hello World to all of you on " << ctime(&now);
cout << os.str();

#ifdef DEPRECATED
char* sbuf = os.str();
delete [] sbuf; // Deallocating dynamically
allocated memory
#else
// ******* Do we need to do something similar for ostringstream?
*******
#endif // DEPRECATED
}
 
R

Rolf Magnus

Generic said:
With the deprecated ostrstream class, when the constructor is invoked
without arguments, memory is dynamically allocated. In that case the
onus on freeing the memory lies with the user. Typically this is done
by obtaining the char buffer (by invoking the str() method) and then
explicitly deleting it.

Does the ostringstream class also have the same issue?

No. It handles its memory automatically.
I mean, if I instantiate ostringstream without any constructor arguments,
is the onus of freeing the dynamically allocated memory still on me?
No.

main()

Btw: Functions without a return type are not just deprecated, but not
allowed at all in C++.
 
U

usenet

No. It handles its memory automatically.

I have seen several snippets of code where programmers have dome
something like this:
ostringstream strstrm;
strstrm << ......;
strstrm.str(""); // <<----------<<


So, is the third statement really uncalled for?

Thanks,
Song
 
M

Marcus Kwok

In said:
I have seen several snippets of code where programmers have dome
something like this:
ostringstream strstrm;
strstrm << ......;
strstrm.str(""); // <<----------<<


So, is the third statement really uncalled for?

Well, it depends on what it's being used for. Sometimes if the
stringstream is being reused, then strstrm.str(""); followed by
strstrm.clear(); will allow you to reuse it (instead of constructing a
new one, for example in the body of a loop). But it is not necessary to
use it to reclaim memory, since the stringstream will take care of it.
 

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

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top