title: Stack allocation in function wrapper / alloca in function
body: <p>I'm looking for a way to wrap stack allocations in abstract data types. For example, I'd like to have a vector which can work strictly via allocations on the stack. My biggest hurdle of course is that <code>alloca</code> works only within the current stack frame -- thus I don't see an easy way to wrap this into a function.</p>
  
  <p>So far the only way I see to do this is by using macro-like functions which are guaranteed to be compiled into a given stack frame. I don't like this approach since it isn't as type friendly as one would hope, and requires more verbose naming than desired.</p>
  
  <p>Is there anyway I can get a function to allocate on its caller stack? I understand this would normally destroy the immediately calling stack, thus likely the function would also have to be forced inline somehow.  I'm not clear on what options I have, so I'm looking for some ideas, or pointers towards possible options.</p>
  
  <hr>
  
  <p><em>Notes:</em></p>
  
  <p>The ultimate goal is something like a <code>std::vector</code> which works strictly on the immediate functions stack. Obviously it would only be passed as a <code>const</code> object to callees, and its life ends with the function.</p>
  
  <p>C approach is fine so long as it is better than my macro based approach. Though some support macros are also acceptable.</p>
  
  <p>I understand this is a fairly specific optimization, and optimally I'd like to be able to (with a flag) turn it on/off (using just an normal std::vector for debugging). It would give a minor speed boost to significant parts of our code, but probably not enough to justify making it unreadable via too many odd constructs.</p>
  
  <p><strong>Answer</strong>: Is most likely that it isn't possible and that only the macro approach would work.</p>