Re: Runtime stack allocation



Stephen Sprunk <stephen@xxxxxxxxxx> writes:
[...]
malloc()/free() should be fast enough, even if it doesn't "feel" like
the right tool for the job. If not, can you use a VLA? If not, does
your system offer alloca()? Any of those are better than subverting
the compiler (and portability) and confusing people who will read your
code later by using inline assembly.

Yes, but alloca() isn't much better. Quoting the alloca man page on
one system:

The alloca function is machine and compiler dependent. On many
systems its implementation is buggy. Its use is discouraged.

On many systems alloca cannot be used inside the list of
arguments of a function call, because the stack space reserved
by alloca would appear on the stack in the middle of the space
for the function arguments.

And VLAs have the drawback that there's no way to check whether the
allocation succeeded.

Use malloc(), and remember to free() the space when you're done with
it.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: RfD - Local buffers, v4 (long)
    ... a VLA: you can just use a fixed-size buffer with that maximum ... the UNIX world -- used alloca() for variable-size local buffers. ... For instance setjmp, which is fully standard. ...
    (comp.lang.forth)
  • Re: RfD - Local buffers, v4 (long)
    ... fixed-size local buffers, ... Why VLA got implemented in ... UNIX world -- used alloca() for variable-size local buffers. ... not use VLA when I program in C, for the reasons stated above. ...
    (comp.lang.forth)
  • Re: Newb- Global Array
    ... Martin Dickopp wrote: ... alloca() can return NULL (in principle, anyway, I don't know if ... > VLA were available. ... Allocate memory that will be automatically released when the current ...
    (comp.lang.c)
  • Re: Threads and VLAs
    ... Aggelidis Nikos schrieb: ... Each thread has its own stack. ... a gloval vla. ... VLAs are allocated by alloca. ...
    (comp.programming.threads)
  • Re: Threads and VLAs
    ... your VLA before the variable gets out of scope everything is OK. ... VLAs are allocated by alloca. ... if (mem) { ... A pointer to the first element and two size_t vars with the dimensions will fit your needs. ...
    (comp.programming.threads)