Surprised no one mentioned this, but you could genarlize that algorithm for any type, using templates.
Like so:
template<typename T>
std::string to_string(const T& obj)
{
std::ostringstream s;
s << obj;
return s.str();
}
That way you can easily convert to a string, like so:
int x = 5;
to_string(x); // will convert the integer to a string
to_string(32); // constants works
to_string<double>(302); // you can explicitly type the type you wish to convert
Example usage is on this link.
Also, as stated by other people, there's no memory going anywhere. Also, I would like to mention, that since you're only writing to a string stream, perhaps you should use std::ostringstream for (1) further clarification when people read your code and (2) no mistakes of using >> instead of <<