memory allocation question when using ostringstream in STL

S

sylcheung

Hi,

I am new to STL, and I have a memory allocation regarding using
ostringstream in STL.

I have a class called 'Rect' and it has a method like this:

string Rect::toString() {
ostringstream ost;
ost << "x:" << x;
ost << " y:" << y;
ost << " w:" << w;
ost << " h:" << h;

return ost.str();
}

And in my program, I call toString() like this:
Rect r;
cout << r.toString();

my question is who free up the memory used by the string return by
ost.str()?

Thank you.
 
P

Peter Jansson

Hi,

I am new to STL, and I have a memory allocation regarding using
ostringstream in STL.

I have a class called 'Rect' and it has a method like this:

string Rect::toString() {
ostringstream ost;
ost << "x:" << x;
ost << " y:" << y;
ost << " w:" << w;
ost << " h:" << h;

return ost.str();
}

And in my program, I call toString() like this:
Rect r;
cout << r.toString();

my question is who free up the memory used by the string return by
ost.str()?

Thank you.

The clean-up is performed by your C++ implementation, i.e. you do not
have to do it yourself.

Regards,
Peter Jansson
 
S

sylcheung

Thanks.

But the variable "ost" (allocate on the stack) is out of scope as seen
as the toString() is returned.
then how can the value 'ost.str()' still be valid after the function
toString() is returned?

Thank you.
 
S

Shezan Baig

But the variable "ost" (allocate on the stack) is out of scope as seen
as the toString() is returned.
then how can the value 'ost.str()' still be valid after the function
toString() is returned?


The toString() function returns by value. You can always return local
stuff by value. If you return by reference, then it will not work.

Hope this helps,
-shez-
 
S

Stephen Howe

But the variable "ost" (allocate on the stack) is out of scope as seen
as the toString() is returned.
then how can the value 'ost.str()' still be valid after the function
toString() is returned?

It isn't. A temporary string is initialised with the copy of ost.str(), ost
is destructed and it is the temporary string that is returned by toString().

Stephen Howe
 
D

David Harmon

On 10 Jan 2006 11:21:27 -0800 in comp.lang.c++, (e-mail address removed)
wrote,
And in my program, I call toString() like this:
Rect r;
cout << r.toString();

my question is who free up the memory used by the string return by
ost.str()?

The return value from r.toString() is in the form of a temporary
string object in the context of the full expression. It is a copy
of the string from inside the function (although actual copying may
be optimized away by very clever compiler.)
At the end of the full expression the temporary is destroyed and its
string class destructor frees any memory that it is holding.
 
M

Marcus Kwok

I have a class called 'Rect' and it has a method like this:

string Rect::toString() {
ostringstream ost;
ost << "x:" << x;
ost << " y:" << y;
ost << " w:" << w;
ost << " h:" << h;

return ost.str();
}

And in my program, I call toString() like this:
Rect r;
cout << r.toString();

Others have answered your memory questions, but I thought I'd point out
something, since you said you're new to STL.

Usually, the "C++ way" of doing this is to define operator<< for your
class, so that you can output the data to any ostream:


#include <iostream>

class Rect {
public:
friend std::eek:stream& operator<<(std::eek:stream& o, const Rect& r);
Rect() : x(0), y(0), w(0), h(0) { }
private:
int x;
int y;
int w;
int h;
};

std::eek:stream& operator<<(std::eek:stream& o, const Rect& r)
{
return o << "x:" << r.x
<< " y:" << r.y
<< " w:" << r.w
<< " h:" << r.h;
}

int main()
{
Rect r;
std::cout << r << '\n';

return 0;
}
 

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,015
Latest member
AmbrosePal

Latest Threads

Top