Reputation: 157
I am trying to write a function that is passed a function to use for allocation as its argument; it should accept any valid allocator of type void *(*)(size_t)
. However I am experiencing strange behavior when attempting to use alloca
as the allocator - constructing a function pointer to the alloca
function compiles fine but results in linker errors:
#include <stdlib.h>
#include <alloca.h>
int main() {
void *(*foo)(size_t) = alloca;
}
results in
/tmp/cc8F67yC.o: In function `main':
test15.c:(.text+0x8): undefined reference to `alloca'
collect2: error: ld returned 1 exit status
Does this have something to do with alloca being inlined? But wouldn't inlining only be done as an optimization when the function doesn't need to have an address. In fact, with GCC I can even write my own version that does work as expected in the above code:
static inline void *alloca(size_t n) {
return __builtin_alloca(n);
}
Is there a reason why the standard version doesn't behave the same way?
Upvotes: 4
Views: 954
Reputation: 144951
You cannot do what you propose. alloca
is a very special beast, it can only be called explicitly inside a function body and not within the argument expressions of a function call.
Note that there is no standard version of alloca
. Neither the C Standard nor POSIX describe this function.
The alternative you expose, with alloca
redefined as an inline function calling __builtin_alloca
does not work: among other problems, the pointer returned by __builtin_alloca()
is only valid until the caller returns, whether it is inlined or not.
The linux man page is very explicit:
[...]
DESCRIPTION
The
alloca()
function allocates size bytes of space in the stack frame of the caller. This temporary space is automatically freed when the function that calledalloca()
returns to its caller.RETURN VALUE
The
alloca()
function returns a pointer to the beginning of the allocated space. If the allocation causes stack overflow, program behavior is undefined.[...]
CONFORMING TO
This function is not in POSIX.1.
There is evidence that the
alloca()
function appeared in 32V, PWB, PWB.2, 3BSD, and 4BSD. There is a man page for it in 4.3BSD. Linux uses the GNU version.NOTES
The
alloca()
function is machine- and compiler-dependent. For certain applications, its use can improve efficiency compared to the use ofmalloc(3)
plusfree(3)
. In certain cases, it can also simplify memory deallocation in applications that uselongjmp(3)
orsiglongjmp(3)
. Otherwise, its use is discouraged.Because the space allocated by
alloca()
is allocated within the stack frame, that space is automatically freed if the function return is jumped over by a call tolongjmp(3)
orsiglongjmp(3)
.The space allocated by
alloca()
is not automatically deallocated if the pointer that refers to it simply goes out of scope.Do not attempt to
free(3)
space allocated byalloca()
!Notes on the GNU version
Normally, gcc(1) translates calls to
alloca()
with inlined code. This is not done when either the-ansi
,-std=c89
,-std=c99
, or the-std=c11
option is given and the header<alloca.h>
is not included. Otherwise, (without an-ansi
or-std=c*
option) the glibc version of<stdlib.h>
includes<alloca.h>
and that contains the lines:#ifdef __GNUC__ #define alloca(size) __builtin_alloca (size) #endif
with messy consequences if one has a private version of this function.
The fact that the code is inlined means that it is impossible to take the address of this function, or to change its behavior by linking with a different library.
The inlined code often consists of a single instruction adjusting the stack pointer, and does not check for stack overflow. Thus, there is no
NULL
error return.BUGS
There is no error indication if the stack frame cannot be extended. (However, after a failed allocation, the program is likely to receive a
SIGSEGV
signal if it attempts to access the unallocated space.)On many systems
alloca()
cannot be used inside the list of arguments of a function call, because the stack space reserved byalloca()
would appear on the stack in the middle of the space for the function arguments.
Upvotes: 2
Reputation: 133978
Who says your function
static inline void *alloca(size_t n) {
return __builtin_alloca(n);
}
works? The object allocated by __builtin_alloca
meets its lifetime at the end of the function so as soon as you return it, you've got a dangling pointer already!
Upvotes: 8
Reputation: 3774
Quoting the man pages from here:
The fact that the code is inlined means that it is impossible to take the address of this function, or to change its behavior by linking with a different library.
The page also mentions:
messy consequences if one has a private version of this function
Upvotes: 6