473,654 Members | 3,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memory allocation question when using ostringstream in STL

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.

Jan 10 '06 #1
6 3142
sy*******@gmail .com wrote:
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
Jan 10 '06 #2
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.

Jan 10 '06 #3
sy*******@gmail .com wrote:
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-

Jan 10 '06 #4
> 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
Jan 10 '06 #5
On 10 Jan 2006 11:21:27 -0800 in comp.lang.c++, sy*******@gmail .com
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.
Jan 10 '06 #6
sy*******@gmail .com wrote:
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::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
Jan 10 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2585
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on Solaries OS? This means that I use new to allocate memory in one Thread and doesn't use delete to release them.
9
2341
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but not necessary give it back to the OS. It seems that .NET win app will only return memory to the OS when the OS is asking for it. But!!! When the OS is asking for it is usually too late, tons of swapping and slow performance.
4
6766
by: PaulR | last post by:
Hi, We have a Server running SLES 8 and 3GB memory, with 1 DB2 instance and 2 active Databases. General info... DB2level = "DB2 v8.1.0.72", "s040914", "MI00086", and FixPak "7" uname -a = Linux galahad 2.4.19-64GB-SMP #1 SMP /etc/sysctl.conf kernel.shmmax=268435456
62
17740
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image(); img.src = ; Then I use the image a few more times by adding it into an Array object:
66
3607
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if (function(socket, args) == -1) { perror("function"); exit(EXIT_FAILURE); } I feel that the ifs destroy the readability of my code. Would it be
24
19068
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array is faster than malloc, but dynamic memory allocation is more flexible. Please comment... thanks.
3
4069
by: Generic Usenet Account | last post by:
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...
13
4343
by: Pep | last post by:
I have recently eradicated a lot of memory leaks in a very old C++ source set. However, whilst they were all fairly easy to resolve, I am confused by the last one. This seems to be related to throwing a std::exception with a string object. This is the test program ============================================================================= #include <sstream> #include <iostream>
10
1461
by: DeveloperDave | last post by:
Hi, I am trying to improve some code that I currently have. I have a simple class called RequestMessage e.g. class RequestMessage { public: RequestMessage();
0
8379
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8294
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8709
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8596
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7309
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1597
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.