Connecting Tech Pros Worldwide Forums | Help | Site Map

memory allocation question when using ostringstream in STL

sylcheung@gmail.com
Guest
 
Posts: n/a
#1: Jan 10 '06
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.


Peter Jansson
Guest
 
Posts: n/a
#2: Jan 10 '06

re: memory allocation question when using ostringstream in STL


sylcheung@gmail.com wrote:[color=blue]
> 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.
>[/color]

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

Regards,
Peter Jansson
sylcheung@gmail.com
Guest
 
Posts: n/a
#3: Jan 10 '06

re: memory allocation question when using ostringstream in STL


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.

Shezan Baig
Guest
 
Posts: n/a
#4: Jan 10 '06

re: memory allocation question when using ostringstream in STL


sylcheung@gmail.com wrote:[color=blue]
> 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?[/color]


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-

Stephen Howe
Guest
 
Posts: n/a
#5: Jan 10 '06

re: memory allocation question when using ostringstream in STL


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

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


David Harmon
Guest
 
Posts: n/a
#6: Jan 10 '06

re: memory allocation question when using ostringstream in STL


On 10 Jan 2006 11:21:27 -0800 in comp.lang.c++, sylcheung@gmail.com
wrote,[color=blue]
>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()?[/color]

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.


Marcus Kwok
Guest
 
Posts: n/a
#7: Jan 10 '06

re: memory allocation question when using ostringstream in STL


sylcheung@gmail.com wrote:[color=blue]
> 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();[/color]

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::ostream& operator<<(std::ostream& o, const Rect& r);
Rect() : x(0), y(0), w(0), h(0) { }
private:
int x;
int y;
int w;
int h;
};

std::ostream& operator<<(std::ostream& 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;
}

--
Marcus Kwok
Closed Thread