2

Is there a way to build an allocator that stores its objects on stack, not the heap? For example, this code may be available via this allocator: std::vector<int, my_allocator<int> > vec;

M.R.Karimi
  • 95
  • 9
  • Do you mean something like std::array in C++11? – ilent2 May 23 '14 at 12:22
  • 1
    Perhaps some abuse of `alloca`, but no, don't even go there. – chris May 23 '14 at 12:25
  • @ilent2 Yes. But I need an "allocator" approach. – M.R.Karimi May 23 '14 at 12:27
  • @chris be sure and put that in a high-count loop construct if you go the alloca route. Always a good time with that one. =P – WhozCraig May 23 '14 at 12:27
  • 1
    Perhaps http://home.roadrunner.com/~hinnant/stack_alloc.html will help. It creates an allocator that uses a block of memory on the stack. If it runs out, it falls back on the standard allocator. Note, the area of memory it uses is pretty much assumed to be local, so you can't return a container with this type, obviously. – Dave S May 23 '14 at 12:30
  • Pretend you have it. How does it deal with a function calling `resize` then returning? – Yakk - Adam Nevraumont May 23 '14 at 12:30
  • There may be a fixed-size array in the allocator, so that objects can be allocated and deallocated in the array. – M.R.Karimi May 23 '14 at 12:38

0 Answers0