Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

0 views
Skip to first unread message

Avalon1178

unread,
May 17, 2007, 1:35:08 PM5/17/07
to avalo...@aol.com
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

Victor Bazarov

unread,
May 17, 2007, 2:38:11 PM5/17/07
to
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


Avalon1178

unread,
May 17, 2007, 3:06:46 PM5/17/07
to
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:

BobR

unread,
May 17, 2007, 6:52:37 PM5/17/07
to

Avalon1178 <Avalo...@aol.com> wrote 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::endl;
test.clear();
cout<<"test.size()"<<test.size()<<std::endl;
cout<<"test.capacity()"<<test.capacity()<<std::endl;
test = "";
cout<<"test.size()"<<test.size()<<std::endl;
cout<<"test.capacity()"<<test.capacity()<<std::endl;
std::string().swap( test );
cout<<"test.size()"<<test.size()<<std::endl;
cout<<"test.capacity()"<<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


Richard Herring

unread,
May 22, 2007, 12:50:57 PM5/22/07
to
In message <1179428805.9...@n59g2000hsh.googlegroups.com>,
Avalon1178 <Avalo...@aol.com> writes
>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

MacBe...@gmail.com

unread,
Jun 11, 2007, 3:05:17 PM6/11/07
to
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 <junk@[127.0.0.1]> wrote:
> In message <1179428805.940880.148...@n59g2000hsh.googlegroups.com>,Avalon1178<Avalon1...@aol.com> writes

James Kanze

unread,
Jun 11, 2007, 4:49:08 PM6/11/07
to
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: james...@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

Old Wolf

unread,
Jun 11, 2007, 11:51:27 PM6/11/07
to
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.

Gennaro Prota

unread,
Jun 12, 2007, 11:19:21 AM6/12/07
to
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)

James Kanze

unread,
Jun 13, 2007, 4:33:54 AM6/13/07
to
On Jun 12, 5:19 pm, Gennaro Prota <addr...@yahoo.com> wrote:
> 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:james...@gmail.com

Gennaro Prota

unread,
Jun 13, 2007, 8:27:08 AM6/13/07
to
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.

Avalon1178

unread,
Jun 14, 2007, 1:20:36 AM6/14/07
to
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()?

James Kanze

unread,
Jun 14, 2007, 3:50:16 AM6/14/07
to
On Jun 14, 7:20 am, Avalon1178 <Avalon1...@gmail.com> wrote:
> 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 "".

0 new messages