Andrey Tarasevich said:
Eric said:
... the function may receive a stack that looks like
[ FILE* ]
[ size_t ]
[ b ]
[ u ]
[ f ]
[ f ]
[ e ]
[ r ]
[ char* ]
... with unwelcome consequences. In order to avoid this,
the compiler needs to special-case the handling of function
arguments involving alloca(), rewriting to something like
char *_temp = alloca(SIZE);
ptr = fgets(_temp, SIZE, stdin);
...
This is, of course, a fake argument. A compiler that provides the
alloca' as a library extension has to make sure that the argument
passing mechanism works correctly. How it does that it its own
headache.
For example, a compiler can decide to _always_ perform the calls to
argument-forming functions before starting to form the next stack
frame. (no need to single out the 'alloca').
No, a compiler that provides alloca() *doesn't* have to make sure that
the argument passing mechanism works correctly. Since no standard
defines the behavior of alloca(), a compiler is free to leave its
behavior undefined in any circumstances where making it work would be
inconvenient.
And in fact that's what at least some compilers actually do. On one
system (Solaris), the man page for alloca says:
The alloca() function is machine-, compiler-, and most of all,
system-dependent. Its use is strongly discouraged.
On another (Ubuntu), it says:
The alloca() function is machine and compiler dependent. On many
sys- tems 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, unlike malloc, it doesn't return a null pointer to indicate
failure; if there's not enough space for the allocation, the behavior
is undefined.