Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the standard guarantee, that std::string::resize will not do reallocate memory, if the new size is less than or equal to as the old one?

Tags:

c++

string

resize

I need to frequently make a string empty and then append some chars into it. std::string::clear() may realloc Does std::string::resize(0) do realloc? The standard's words didn't garentee any about it.

like image 765
rockeet Avatar asked May 09 '13 13:05

rockeet


1 Answers

I think the best possible answer to this is the "Notes" section at http://en.cppreference.com/w/cpp/string/basic_string/clear.

Unlike for std::vector::clear, the C++ standard does not explicitly require that capacity is unchanged by this function, but existing implementations do not change capacity.

And if the capacity is unchanged, that would almost certainly mean that no allocation or freeing functions are called. That's probably the best you can do, short of looking at each and every implementation you care about.

like image 173
Nir Friedman Avatar answered Oct 04 '22 01:10

Nir Friedman