473,795 Members | 3,215 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 #1
12 4292
Avalon1178 wrote:
[..large string in a program needs to be cleared at some point..]

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.
Who said it is zeroing the buffer? Did you see your implementation
actually performing zeroing? Or is it your speculation?
My question is, is this
wise?
Is *what* wise? Premature optimisation? Optimisation based on
a speculation instead of a measurement?
Despite this large record, is it advised to use .clear()
regardless rather than setting it to empty string?
In most cases they will be equivalent or the difference is not going
to be noticeable in the overall program execution.
Any potential
implications on this?
Huh?
I ran valgrind on my application and it doesn't
report a memory leak if I just set the variable to empty string....
Good. Now, if you really need to know the performance difference,
pull out a profiler and actually measure the time.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 17 '07 #2
WTF?

You don't need to ramble on and critique the side questions to show
off what you know! Damn, did you write std::string or something that
some speculation I said ruffled your feathers? Geez! Drink some kool-
aid to cool off....

In any case, this is the only response I'm looking for:
>
Despite this large record, is it advised to use .clear()
regardless rather than setting it to empty string?

In most cases they will be equivalent or the difference is not going
to be noticeable in the overall program execution.
May 17 '07 #3

Avalon1178 <Av********@aol .comwrote in message ...
>
In any case, this is the only response I'm looking for:
Despite this large record, is it advised to use .clear()
regardless rather than setting it to empty string?
In most cases they will be equivalent or the difference is not going
to be noticeable in the overall program execution.
{ using std::cout; // for NG post // main or function
std::string test("This is a string.");
cout<<"test.siz e()"<<test.size ()<<std::endl;
cout<<"test.cap acity()"<<test. capacity()<<std ::endl;
test.clear();
cout<<"test.siz e()"<<test.size ()<<std::endl;
cout<<"test.cap acity()"<<test. capacity()<<std ::endl;
test = "";
cout<<"test.siz e()"<<test.size ()<<std::endl;
cout<<"test.cap acity()"<<test. capacity()<<std ::endl;
std::string().s wap( test );
cout<<"test.siz e()"<<test.size ()<<std::endl;
cout<<"test.cap acity()"<<test. capacity()<<std ::endl;
}
/* - output -
test.size()17
test.capacity() 17
test.size()0
test.capacity() 17
test.size()0
test.capacity() 17
test.size()0
test.capacity() 0
*/

--
Bob R
POVrookie
May 17 '07 #4
In message <11************ **********@n59g 2000hsh.googleg roups.com>,
Avalon1178 <Av********@aol .comwrites
>WTF?
Please don't top-post.
>
You don't need to ramble on and critique the side questions to show
off what you know! Damn, did you write std::string or something that
some speculation I said ruffled your feathers?
You didn't speculate, you implied that you *knew* that calling clear()
would "zero out" a string. That's a statement that could potentially
mislead other people reading your post. Anyone who points out that this
was mere speculation on your part is performing a positive service to
them.
>Geez! Drink some kool-
aid to cool off....
You received valuable information and some good advice at no charge, so
maybe it's _you_ who should be cooling off.

--
Richard Herring
May 22 '07 #5
Well, I think I can understand Avalon's rant. If someone were to
answer with a smart-alec response that Bazarov did, I think I would be
pi$$ed off too. A straightforward answer like what Herring said may
have averted these quarrels...

Anyway, kool aids aside, the question did got me curious. How DOES it
"clean" an stl string with a "" versus a clear() if clear() is not
zeroing it out?

On May 22, 9:50 am, Richard Herring <ju**@[127.0.0.1]wrote:
In message <1179428805.940 880.148...@n59g 2000hsh.googleg roups.com>,Aval on1178<Avalon1. ..@aol.comwrite s
WTF?

Please don't top-post.
You don't need to ramble on and critique the side questions to show
off what you know! Damn, did you write std::string or something that
some speculation I said ruffled your feathers?

You didn't speculate, you implied that you *knew* that calling clear()
would "zero out" a string. That's a statement that could potentially
mislead other people reading your post. Anyone who points out that this
was mere speculation on your part is performing a positive service to
them.
Geez! Drink some kool-
aid to cool off....

You received valuable information and some good advice at no charge, so
maybe it's _you_ who should be cooling off.

--
Richard Herring

Jun 11 '07 #6
On Jun 11, 9:05 pm, MacBeth2...@gma il.com wrote:
Well, I think I can understand Avalon's rant. If someone were to
answer with a smart-alec response that Bazarov did, I think I would be
pi$$ed off too. A straightforward answer like what Herring said may
have averted these quarrels...
I wouldn't worry about it. If you read this group even a
little, you'll see that that's just Bazarov's style. Just
ignore him if it bothers you.
Anyway, kool aids aside, the question did got me curious. How DOES it
"clean" an stl string with a "" versus a clear() if clear() is not
zeroing it out?
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.

The question, of course, is: do you want them to free all of
their memory. If the container is going to be reused, and end
up with just as many elements as before, you'll just have to
reallocate it. In many cases, it is preferable to just
"logically" free the elements, and let the container hold on to
the memory it has for the next time around.

--
James Kanze (Gabi Software) email: ja*********@gma il.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 11 '07 #7
On Jun 12, 7:05 am, MacBeth2...@gma il.com wrote:
Anyway, kool aids aside, the question did got me curious.
How DOES it "clean" an stl string with a "" versus a clear()
if clear() is not zeroing it out?
By setting the length to 0. C++ standard strings
use a length count. (Note that 'STL' is an anachronism;
since the C++ standard was published, std::string is
part of the C++ Standard Library).

Also note that this operation usually doesn't free
memory; if you want to free memory then do the
swap trick mentioned by others.

Jun 12 '07 #8
On Mon, 11 Jun 2007 20:49:08 -0000, James Kanze wrote:

[...]
>Anyway, kool aids aside, the question did got me curious. How DOES it
"clean" an stl string with a "" versus a clear() if clear() is not
zeroing it out?

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) .
>The question, of course, is: do you want them to free all of
their memory. If the container is going to be reused, and end
up with just as many elements as before, you'll just have to
reallocate it. In many cases, it is preferable to just
"logically" free the elements, and let the container hold on to
the memory it has for the next time around.
In effect, I wrote a function template similar to the above (called
"reinitiali ze", FWIW) but I never had a chance to use it. That's why
it isn't even online. I just checked and it has the following comment
"See also LWG issues 225, 226, 229 and N1387 (last checked 4 Jan
2006)". That means that last time I looked at the code was almost 18
months ago :-)

--
Gennaro Prota -- Need C++ expertise? I'm available
https://sourceforge.net/projects/breeze/
(replace 'address' with 'name.surname' to mail)
Jun 12 '07 #9
On Jun 12, 5:19 pm, Gennaro Prota <addr...@yahoo. comwrote:
On Mon, 11 Jun 2007 20:49:08 -0000, James Kanze wrote:
[...]
Anyway, kool aids aside, the question did got me curious. How DOES it
"clean" an stl string with a "" versus a clear() if clear() is not
zeroing it out?
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.
In practice (although I don't think even that is absolutely
guaranteed), this will result in the container c having exactly
the same state as a just constructed object, whatever that is.
In most of the implementations I've worked with, this will mean
no allocated memory for std::vector, and very little in general.
(I'm not 100% sure, but I seem to remember noting that the g++
implementation of std::list did allocate a node in the default
constructor. And of course, many modern implementations of
std::basic_stri ng always have a minimum capacity greater than
0.)

--
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 13 '07 #10

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

Similar topics

11
4977
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
7499
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
1812
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
2058
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
2269
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
8377
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
7964
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
10439
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
10215
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
10165
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
9043
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
6783
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
5437
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...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.