11

I have two questions:

1) Is it possible to implement an allocator that uses alloca to allocate memory on the stack and is otherwise C++ STL compliant?

If there is code out there, you can make me happy by simply pointing me to the URL. :-) If there is no code out there, perhaps you can sketch the functions allocate and deallocate?

2) If the answer to the above question is 'yes', I'd like to understand how it is possible to allocate memory on the stack for class members. As an example, consider an

std::vector<int, AllocaAllocator<int> > 

and suppose that a call of the member function 'resize' of this vector calls first 'deallocate' and then 'allocate' of the allocator.

The scope from which allocate is called is that of the member function resize. Doesn't this mean that the allocated memory is removed from the stack at the end of that function call?

Kind regards, Bjoern

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
Bjoern
  • 119
  • 3
  • 3
    You can't deallocate space that you have allocated on the stack via alloca. The only way to get of memory allocated by alloca is to exit the function. – Patrick Nov 02 '10 at 22:11
  • Does this mean that, if you exit a function, all memory allocated within that function on the stack via alloca is freed? And does this imply that it is impossible to use alloca to allocate memory for class members? Thus, is it impossible to write an allocator that uses alloca? – Bjoern Nov 02 '10 at 22:19
  • 2
    Please have a look at this question: "Looking for C++ STL-like vector class but using stack storage" (http://stackoverflow.com/q/354442) There are already some valuable comments on that topic. – Bernhard Kausler Nov 03 '10 at 08:57
  • The real question is why do you want to do this? – Martin York Dec 14 '10 at 19:25
  • possible duplicate of [Looking for C++ STL-like vector class but using stack storage](http://stackoverflow.com/questions/354442/looking-for-c-stl-like-vector-class-but-using-stack-storage) – user207421 Nov 09 '11 at 09:06
  • @Bjoern Yes it does. That's the whole point of `alloca()`. If you don't know that why you do think you want to use it? – user207421 Nov 09 '11 at 09:06

2 Answers2

6

Bjoern, it looks like you fundamentally misunderstand how stack and alloca work. Read about them.

What you are asking is impossible because the memory allocated by alloca is "freed" when you return from the function that allocated it (and unlike Patrick said, inlining cannot change its behavior). I write "freed" because it's not actually freed, it just goes out of scope as any other stack variable. So using it afterwards causes undefined behavior.

Suppose you allocate a chunk of memory in YourAllocator::allocate which is called from d.push_back():

deque<int, AllocaAllocator> d;
d.push_back(42); // calls alloca
printf("Hello\n");
printf("%d\n", d[0]);

The memory allocated by alloca may be overwritten by the stack-frames of push_back and printf, so the output may not be 42, it may crash, or any other thing.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
4

No, this kind of thing isn't possible. For a start, the STL expects to allocate more memory, then free the old memory. How are you going to do that on the stack?

The only thing even remotely equivalent to this is a conservative garbage collector.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 2
    +1 for "the STL expects to allocate more memory, *then* free the old memory. How are you going to do that on the stack?" – Martin Ba Nov 08 '11 at 06:53