Before I write my own, I will ask all of you.
I am looking for a C ++ class that is almost exactly like an STL vector, but stores data in an array on the stack. Some class of STL allocator will work, but I try to avoid any heap, even static distributed cumulus (though one of them is my second choice). The stack is more efficient.
It should be almost a replacement for the current code that uses the vector.
For what I was going to write myself, I was thinking about something like this:
char buffer[4096]; stack_vector<match_item> matches(buffer, sizeof(buffer));
Or the class may contain buffer space inside. Then it will look like this:
stack_vector<match_item, 256> matches;
I thought he would throw std :: bad_alloc if he runs out of free space, although this should not happen.
Update
Using Chromium stack_container.h works great!
The reason I didnβt think I was doing it myself was because I always ignored the allocator object parameter for the STL collection constructors. I used the template parameter several times to create static pools, but I never saw the code or wrote any that actually used the object parameter. I learned something new. Very cool!
The code is a bit messy and for some reason GCC made me declare the allocator as the actual element, rather than constructing it into a vector allocator parameter. It came from something like this:
typedef std::pair< const char *, const char * > comp_list_item; typedef std::vector< comp_list_item > comp_list_type; comp_list_type match_list; match_list.reserve(32);
For this:
static const size_t comp_list_alloc_size = 128; typedef std::pair< const char *, const char * > comp_list_item; typedef StackAllocator< comp_list_item, comp_list_alloc_size > comp_list_alloc_type; typedef std::vector< comp_list_item, comp_list_alloc_type > comp_list_type; comp_list_alloc_type::Source match_list_buffer; comp_list_alloc_type match_list_alloc( &match_list_buffer ); comp_list_type match_list( match_list_alloc ); match_list.reserve( comp_list_alloc_size );
And I have to repeat it when I announce a new one. But it works the way I wanted.
I noticed that stack_container.h has StackVector installed and I tried to use it. But it does not inherit from the vector or does not define the same methods, so it was not a replacement. I did not want to rewrite all the code using a vector, so I abandoned it.