Is there a malloc/free based allocator in the STL? If not, does anyone know of a simple copy/paste one? I need it for a map that must not call new/delete.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First, I’d note that changing the allocator for the map itself won’t change the allocation used by the objects stored in the map. For example, if you do something like:
The map itself will allocate memory using the specified allocator, but when the
std::strings in the map allocate memory, they’ll still use the default allocator (which will usenewanddelete. So, if you need to avoidnewanddeletein general, you have to ensure that not only the map itself uses the right allocator, but that any objects it stores do the same (I know that’s probably stating the obvious, but I’ve overlooked it, so maybe it’s worth mentioning).With that proviso, on with the code:
And, a little test code: