I know that the compiler may, not shall unroll inline
functions into the calling function to avoid the overhead associated with calling out-of-line functions. I also know, however, that inline
functions are linked differently than out-of-line functions, so I can't expect them to act the same way, exactly.
While I am actually using C++, I'm developing a program using an api in which it is convenient to make use of C macros that look something like:
#define func_alloca(ptr) do { *ptr = alloca(size); memset(*ptr, 0, size); }
In the interest of not repeating code several times in different functions, it would be useful for me to be able to functionalize a series of these alloca
calls. My question is, (in gcc specifically, since alloca
is implementation-defined,) can I initialize variables on the stack using alloca
, inside an inline
function, and count on them to work outside? Here's an example of what I want to do:
inline void uses_alloca(type*& ptr){
// do stuff
func_alloca(ptr);
}
void calls_inline(){
type *local_ptr;
uses_alloca(local_ptr);
// is local_ptr valid?
}
If it does work, will it always work? As in, not only when the compiler chooses to unroll the inline
function into the calling function?
Edit:
My guess is that this probably will work in fact, since the alloca
call is already valid outside the do{}
local scope contained in the macro, and since, if I understand correctly, a function doesn't actually have a different level of scope in C++ than a do{}
block. My question remains: is this the way alloca
is supposed to work in gcc? Is this defined behavior?