473,804 Members | 3,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

basic question about "clearing" a string....

Hi,

I have an application that periodically uses a std::string variable
which is assigned a VERY VERY large string (15000000+ bytes long).
This application is essentially a daemon, and it polls a data set
which can have a lot of information and it is concatenated in this
single string variable object. When the daemon finishes its job, it
goes to sleep, but before doing so, it "clears" this variable so it
can be reused again in the next poll.

Currently, when I say clear, all I'm doing to the variable is setting
it to an emtpy string (var = ""), rather than calling the .clear()
member function because of my concern with std::string performance of
actually zeroing out this very large buffer. My question is, is this
wise? Despite this large record, is it advised to use .clear()
regardless rather than setting it to empty string? Any potential
implications on this? I ran valgrind on my application and it doesn't
report a memory leak if I just set the variable to empty string....

Avalon1178

May 17 '07
12 4294
On Wed, 13 Jun 2007 01:33:54 -0700, James Kanze wrote:
>The "standard" idiom for completely clearing a standard
container is to swap it with a just constructed instance, e.g.:
template< typename Container >
void
reset( Container& c )
{
Container().swa p( c ) ;
}
>As far as I know, this is the only way to get certain containers
(including std::basic_stri ng and std::vector) to free all of the
memory they might hold.
>However one shouldn't fall into the trap of believing that the "reset"
container will not hold any memory: even a just-constructed container
may have some excess capacity (it's up to the implementation) .

You'll notice I put "standard" in quotes. I meant "standard" in
the usual, everyday sense, and not as a reference to ISO 14882.
Sure, I got that. The remark wasn't addressed to you; and your post
contained no statement that I saw as incorrect. My only intent was to
add more info on the subject. BTW, a common name for the idiom is
"shrink-to-fit"; I'm not sure whether that name was
invented/popularized by Scott Meyers or Herb Sutter.

--
Gennaro Prota -- Need C++ expertise? I'm available
https://sourceforge.net/projects/breeze/
(replace 'address' with 'name.surname' to mail)
Jun 13 '07 #11
Wow, I wasn't aware this thread is still alive! Thanks for all your
help!

So I take it that "clearing" a string via setting the variable to ""
or calling .clear() has the same effect. What is usually the practice
that is followed if I want to clear the string (not necessarily free
the memory buffer, but just to "clear" it?) Set the string variable
to "" or call clear()?

Jun 14 '07 #12
On Jun 14, 7:20 am, Avalon1178 <Avalon1...@gma il.comwrote:
Wow, I wasn't aware this thread is still alive! Thanks for all your
help!
Somebody woke it up again, with a rant and an additional
question:-).
So I take it that "clearing" a string via setting the variable to ""
or calling .clear() has the same effect. What is usually the practice
that is followed if I want to clear the string (not necessarily free
the memory buffer, but just to "clear" it?) Set the string variable
to "" or call clear()?
Depends on your personal feeling, I think. If I'm thinking in
terms of "clearing" or emptying the string, I'll call clear().
If I'm thinking in terms of an empty string, I'll assign "".

--
James Kanze (GABI Software, from CAI) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 14 '07 #13

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

Similar topics

11
4979
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g., http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=Tidy7IDbDHA.2108%40cpmsftngxa06.phx.gbl) that indicate that ASP will queue up requests when they come in with the same "session".
2
7500
by: Woodster | last post by:
I am using std::stringstream to format a string. How can I clear the stringstream variable I am using to "re use" the same variable? Eg: Using std::string std::string buffer; buffer = "value1" + " : " + "value2";
9
1967
by: /* frank */ | last post by:
I want system ("CLS") if the system is WINDOWS system ("CLEAR") is the OS is a UNIX like.
31
1814
by: Xero | last post by:
I have an array in my program. (Declared as letters(16, 16)). When I paused the program in debugging mode, I saw the following in the Autos window: letters |- (0, 0) | Nothing |- (0, 1) | Nothing
8
2059
by: gw7rib | last post by:
I've been bitten twice now by the same bug, and so I thought I would draw it to people's attention to try to save others the problems I've had. The bug arises when you copy code from a destructor to use elsewhere. For example, suppose you have a class Note. This class stores some text, as a linked list of lines of text. The destructor runs as follows: Note::~Note() {
2
2270
by: alxasa | last post by:
Hello, I am hoping someone can help me with this. I need a javascript function, which sits inside a <input type="text" name="firstname"> line of code. Now, if someone starts typing fine, but when it goes 1 character past 15 characters (15 characters only allowed), in this case I would like the contents of the input to be cleared out (automatically), and reset the input and its value back to nothing. a) Can this be done, and will someone...
25
3021
by: Koliber (js) | last post by:
sorry for my not perfect english i am really f&*ckin angry in this common pattern about dispose: ////////////////////////////////////////////////////////// Public class MyClass:IDisposable
206
8386
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a footprint which says: "Indeed, one often hears arguments against building exibility into an engineered sys- tem. For example, in the philosophy of the computer language Python it is claimed: \There should be one|and preferably only one|obvious...
4
7965
by: =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post by:
Hi all, Recently I found an interesting question on C# forums about clearing event handlers of an event. I tried to give it a solution, but failed. I am interested to know how you guys take this. Here it goes class Product { public event EventHandler ProductChanged;
0
9707
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
9585
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,...
1
10323
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
10082
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
9161
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
7622
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
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4301
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
3
2997
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.