Do separate instances of C ++ std :: string use the same allocator?

One thing I've always thought about is if the std::string instances that I use in my C ++ code use the same allocator or do they have their own separate memory pools?

Obviously, sharing a single memory pool for multiple, often created and destroyed lines is more efficient. Can someone confirm or deny this to me, please?

+7
source share
2 answers

By default, they all use std::allocator , which uses standard memory procedures to get free heap blocks. Unification is not involved on this layer.

(However, most heap implementations use a dedicated heap with low fragmentation to serve small allocations, and strings most likely fall into this category. But it depends on the implementation and is not exclusive or optimized for std::string ...).

+10
source

different C ++ instances use the same allocator unless you specify it. Perhaps you mean string interning, standard in java / python, etc. If so, no. There is no "standard" tool for this. However, this is easy to add if the frequent create / destroy problem is the problem.

http://en.wikipedia.org/wiki/String_interning

+2
source

All Articles