Hi,
I need to know if stack frames are generated in case of a
inline function execution or
do they execute just like macros?
if they execute like macros, then what is the need for having
inline function?
where would you use macros and where inlines?
Inline functions provide the same semantics as real functions:
argument conversions, type checking, automatic referential hygiene,
single evaluation of argument values, and a body with declarations and
multiple returns. Macros don't even ensure that a substituted
expression maintains its integrity with respect to surrounding
operators, which may have a higher precedence than that expression's
principal operator.
A lot of the features of an inline function can be emulated in a macro
if you have the GNU C extension that a { } block in parentheses can be
used as an expression, and combine that with a number of conventions.
That's not ANSI C, so you're out of luck.
Even if you have the GNU C extension ({ .. }), there still the hygiene
problem. What does that mean? Suppose that your macro expansion has to
call some helper functions, foo and bar. And it has to refer to a
global variable called xyzzy:
{
// body of inline function
if (xyzzy) foo() else bar();
}
If this is done as a macro, then you have the lexical capture
problem: the place where the macro is inserted can have its own local
xyzzy variable.
{
int xyzzy = 0;
void (*foo)(void) = &some_function;
MACRO();
}
Now the expansion of the macro refers to the local xyzzy, not the
global one. And when it calls function foo, it's actually calling
through the local pointer here.
This scoping problem doesn't occur with inline functions, which have
their own independent scope just like normal functions.
In C++ you can overcome this problem by using namespaces, and by using
fully-qualified namespace identifiers in your macro expansions. In C,
you have to make sure that your macros refer to some names that are
unlikely to be used elsewhere. That's yet one more convention to
follow in making a nearly bullet-proof macro.