473,408 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Memory deallocation issues with ostringstream

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
}

Jan 24 '07 #1
3 4045
Generic Usenet Account wrote:
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?
No. It handles its memory automatically.
I mean, if I instantiate ostringstream without any constructor arguments,
is the onus of freeing the dynamically allocated memory still on me?
No.
main()
Btw: Functions without a return type are not just deprecated, but not
allowed at all in C++.

Jan 24 '07 #2


On Jan 24, 12:29 pm, Rolf Magnus <ramag...@t-online.dewrote:
Does the ostringstream class also have the same issue?
No. It handles its memory automatically.
I have seen several snippets of code where programmers have dome
something like this:
ostringstream strstrm;
strstrm << ......;
strstrm.str(""); // <<----------<<
So, is the third statement really uncalled for?

Thanks,
Song

Jan 24 '07 #3
In comp.lang.c++ us****@sta.samsung.com wrote:
On Jan 24, 12:29 pm, Rolf Magnus <ramag...@t-online.dewrote:
Does the ostringstream class also have the same issue?
No. It handles its memory automatically.

I have seen several snippets of code where programmers have dome
something like this:
ostringstream strstrm;
strstrm << ......;
strstrm.str(""); // <<----------<<
So, is the third statement really uncalled for?
Well, it depends on what it's being used for. Sometimes if the
stringstream is being reused, then strstrm.str(""); followed by
strstrm.clear(); will allow you to reuse it (instead of constructing a
new one, for example in the body of a loop). But it is not necessary to
use it to reclaim memory, since the stringstream will take care of it.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jan 24 '07 #4

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

Similar topics

2
by: Laurens | last post by:
Hi, My application needs to create in-memory arrays, the size of which is not known, or very hard to calculate, in advance. I would like to stream the data using the standard I/O class...
18
by: Tron Thomas | last post by:
Given the following information about memory management in C++: ----- The c-runtime dynamic memory manager (and most other commercial memory managers) has issues with fragmentation similar to a...
4
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...
11
by: aaaaa | last post by:
Hi all, Does anybody know if STLPort or SGI STL standard allocators do memory pooling for the list, map and set? Also I have had a look at the BOOST pool_alloc (to be used as a pooling...
9
by: Alfonso Morra | last post by:
I have the following data types: typedef union { long l ; double f; char* s ; void* p ; } Value ; typedef struct {
6
by: sylcheung | last post by:
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() {...
0
by: NuclearOnslaught | last post by:
Hey all, I'm running some data analysis scripts that, due to the input file size, tend to be quite memory intensive. I've been reading up on Python's memory allocation (I'm using 2.4 and am...
13
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...
9
by: Steven Powers | last post by:
Imagine the following setup class Parent { virtual void doStuff(); } class Child : public Parent { virtual void doStuff(); }
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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...
0
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,...
0
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...
0
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...

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.