0

The only alloca() replacement for C++ I have found on the Internet looks something like this:

template <typename F>
void alloca_(::std::size_t const n, F&& f) noexcept(noexcept(f({})))
{
  alignas(::std::max_align_t) char p[n];

  f(p);
}

But, obviously, this is not the alloca() we know and love, it is not a function, nor a macro. Can an alloca() implementation that resembles the alloca() function call semantics more closely, be written? Perhaps as a macro? Whether macro or function, it should allocate space from the stack, not the heap.

user1095108
  • 14,119
  • 9
  • 58
  • 116
  • 1
    I don't follow your question. Are you looking for a replacement for stack based allocator ? – Arunmu Jul 28 '16 at 14:31
  • No, just something similar to alloca(), with the funcion call semantics, not like the example, that requires a functor. – user1095108 Jul 28 '16 at 15:00
  • 2
    Is there something wrong with the actual `alloca` that prevents you from using it in C++ code? That's one of the selling points of C++: it can natively interface with C. – Nicol Bolas Jul 28 '16 at 21:33
  • Sometimes alloca() is not available. – user1095108 Jul 29 '16 at 05:27
  • The example doesn't require a functor... Are you saying you want something you can call like `alloca(size, foo);` given `void foo(char* memory);`? That should work as is. – chris Aug 03 '16 at 16:31
  • No, I'd like to see an alloc() reimplementation example with vla, if it is possible. – user1095108 Aug 03 '16 at 17:06

0 Answers0