Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack allocator for C++03 standard containers

For a software I have to avoid any use of memory in the heap, and only rely on stack-allocated memory. Then, this prevents me from using any C++ standard containers, such as vector, map, string (well, basic_string), which I would really like to use, to ease development and data manipulation.

I found (many) implementations of stack allocators, such as this one which itself references two others, or this one from chromium.

Many of them are not fully compliant with the standard, or rely on C++11 (and I am stuck with C++03 at the moment, sadly). Do you have any feedback about a good already existing stack allocator for C++03 or should I adapt one of the above?

Thanks!

like image 679
Phyks Avatar asked Oct 28 '15 14:10

Phyks


People also ask

Which allocator member function do standard containers use to acquire storage for their elements in C++?

The std::allocator class template is the default Allocator used by all standard library containers if no user-specified allocator is provided.

What is a stack allocator?

Note: A stack-like allocator means that the allocator acts like a data structure following the last-in, first-out (LIFO) principle. This has nothing to do with the stack or the stack frame. The stack allocator is the natural evolution from the arena allocator.

Which of the given member function S is are associated with std :: allocator ()?

Member functions associated with std::allocator() : address: It is used for obtaining the address of an object although it is removed in C++20. construct: It is used to construct an object.It is also removed in C++20. destroy: It is used to destruct an object in allocated storage.It is also removed in C++20.

What is allocator type?

Allocators are used by the C++ Standard Library to handle the allocation and deallocation of elements stored in containers. All C++ Standard Library containers except std::array have a template parameter of type allocator<Type> , where Type represents the type of the container element.


1 Answers

Howard Hinnant's short_alloc.h (see also here) is a pretty good start (you will need to add C++03 boilerplate, see here).

Of course, this will still go to the heap if it runs out of memory, the alternative is to throw std::bad_alloc.

like image 187
Walter Avatar answered Sep 22 '22 07:09

Walter