473,785 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4081
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.sams ung.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
5726
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 framework. The class should take care of the underlying memory management. Now which class is most suitable for this? (Assuming there is one.) Will std::ostrstream do the trick? In Java I use java.io.ByteArrayOutputStream to create in-memory data.
18
6681
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 hard drive file system. Over time, the more often use call new/delete or alloc/free, there will be gaps and fragments in the heap. This can lead to inefficient use of available memory, as well as cache-hit inefficiencies.
4
2591
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.
11
2664
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 allocator for lists), but looking into the code it doesn't seem to ever release (to the global ::free) the memory that was once allocated. I can understand that such memory can be re-used if I have another list
9
10800
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
3150
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() { ostringstream ost; ost << "x:" << x;
0
946
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 locked into it, so I don't have the new fixes in 2.5) but because of the nature of its looping I don't think its losing much memory because of some of the deallocation issues - it will reuse the ints and lists that Python is retaining references to.
13
4357
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>
9
2637
by: Steven Powers | last post by:
Imagine the following setup class Parent { virtual void doStuff(); } class Child : public Parent { virtual void doStuff(); }
0
9645
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
9480
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
10325
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
10147
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...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8972
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...
0
6739
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();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.