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?
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