Is std::string guaranteed not to give back memory spontaneously?
Is it guaranteed by the standard that std::string will not give back allocated memory spontaneously if reassigned from a string of a smaller size? In other words: std::string str = "Some quite long string, which needs a lot of memory"; str = ""; str = "A new quite long but smaller string"; // Guaranteed to not result in a heap allocation? I ask because i'm depending on this to avoid heap fragmentation. No guarantee whatsoever. [string.cons]/36 defines assigning a const char* to an std::string in term of a move-assignment, whose definition is: [string.cons]/32 basic_string& operator=(basic_string&& str) noexcept(/*...*/) Effects: Move assigns as a sequence container, except that iterators, pointers and references may be invalidated. This shows that the Committee let the implementation choose freely between an invalidating operation and a more conservative one. CPP reference states that assignment to a pointer-to-char Repla...