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
}
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
}