By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,106 Members | 2,474 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,106 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
12 Replies


P: n/a
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

P: n/a
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

P: n/a

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.size()"<<test.size()<<std::endl;
cout<<"test.capacity()"<<test.capacity()<<std::end l;
test.clear();
cout<<"test.size()"<<test.size()<<std::endl;
cout<<"test.capacity()"<<test.capacity()<<std::end l;
test = "";
cout<<"test.size()"<<test.size()<<std::endl;
cout<<"test.capacity()"<<test.capacity()<<std::end l;
std::string().swap( test );
cout<<"test.size()"<<test.size()<<std::endl;
cout<<"test.capacity()"<<test.capacity()<<std::end l;
}
/* - 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

P: n/a
In message <11**********************@n59g2000hsh.googlegroups .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

P: n/a
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.940880.148...@n59g2000hsh.googlegroups .com>,Avalon1178<Avalon1...@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

Jun 11 '07 #6

P: n/a
On Jun 11, 9:05 pm, MacBeth2...@gmail.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().swap( c ) ;
}

As far as I know, this is the only way to get certain containers
(including std::basic_string 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*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 11 '07 #7

P: n/a
On Jun 12, 7:05 am, MacBeth2...@gmail.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

P: n/a
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().swap( c ) ;
}

As far as I know, this is the only way to get certain containers
(including std::basic_string 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
"reinitialize", 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

P: n/a
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().swap( c ) ;
}
As far as I know, this is the only way to get certain containers
(including std::basic_string 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_string 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 objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 13 '07 #10

P: n/a
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().swap( c ) ;
}
>As far as I know, this is the only way to get certain containers
(including std::basic_string 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

P: n/a
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

P: n/a
On Jun 14, 7:20 am, Avalon1178 <Avalon1...@gmail.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 objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 14 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.