Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 684
Gearoid Murphy Avatar asked Mar 09 '11 23:03

Gearoid Murphy


2 Answers

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

like image 178
Alexander Gessler Avatar answered Sep 20 '22 11:09

Alexander Gessler


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

like image 30
Naveen Avatar answered Sep 22 '22 11:09

Naveen