Gearoid Murphy
Gearoid Murphy

Reputation: 12116

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

One thing I've always wondered about is if the instances of std::string 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 across multiple, frequently created and destroyed strings is more efficient. Can anyone confirm or deny this for me please?

Upvotes: 7

Views: 440

Answers (2)

Naveen
Naveen

Reputation: 4678

different instances of c++ use the same allocator if you don't specify one. You perhaps are referring to string interning that is standard in java/python etc. If so, no. There is no 'standard' facility for that. However it is easy to add if frequent create/destroy is an issue

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

Upvotes: 2

Alexander Gessler
Alexander Gessler

Reputation: 46607

By default, they all use std::allocator, which uses standard memory routines to get free heap blocks. There is no pooling involved on this layer.

(However, most heap implementations use a dedicated low-fragmentation heap to serve small allocations, and strings are most likely to fall into this category. But this is implementation dependent and not exclusive to or optimized for std::strings ...).

Upvotes: 10

Related Questions