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

A

Avalon1178

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
 
V

Victor Bazarov

Avalon1178 said:
[..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
 
A

Avalon1178

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:
 
B

BobR

Avalon1178 said:
In any case, this is the only response I'm looking for:

{ 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
*/
 
R

Richard Herring


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.
 
M

MacBeth2387

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?
 
J

James Kanze

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.
 
O

Old Wolf

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.
 
G

Gennaro Prota

On Mon, 11 Jun 2007 20:49:08 -0000, 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).
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 :)
 
J

James Kanze

]
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.)
 
G

Gennaro Prota

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.
 
A

Avalon1178

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()?
 
J

James Kanze

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 "".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,293
Messages
2,571,505
Members
48,190
Latest member
Alisnrob

Latest Threads

Top