Re: Is there stack associated when a executing an inline function?



Paul Hsieh <websnarf@xxxxxxxxx> writes:
On Feb 28, 11:37 pm, Mahesh <mahesh1...@xxxxxxxxx> wrote:
       I need to know if stack frames are generated in case of a
inline function execution or do they execute just like macros?

The implicit call stack retains all its behaviors as normal regardless
of function call decorations like "inline".

The only thing the standard can say about "inline" is that it prevents
an function's address be stored in a function pointer. For many
cases, this can already be established by the compiler, but this is
useful for compilers that don't. (Its essentially, a new keyword,
helpful for old compilers that will not be updated to the new
standard. Don't ask me; I had no hand in this.)

No, the standard doesn't prohibit taking the address of an inline
function. (Perhaps you're thinking of "register".)

I'm not sure what you mean when you say this is "helpful for old
compilers that will not be updated to the new standard". Compilers
that don't support C99 (more specifically, compilers that conform to
C90) don't support the "inline" keyword.

      if they execute like macros, then what is the need for having
inline function?

macros just perform text substitutions, and cannot call themselves
recursively.

where would you use macros and where inlines?

You use macros to avoid call overhead and when text substitution is
sufficient. Macros also have certain compile-time capabilities that
you don't get from function calls: stringification with # x and text
concatenation via: x ## y.

Macros can also be dangerous when used carelessly. My favorite
example of macro abuse is:

#include <stdio.h>

#define SIX 1+5
#define NINE 8+1

int main(void)
{
printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
return 0;
}

That's not to say that macros can't be used safely, but they're one of
the sharper tools in the C toolbox, and more elaborate precautions are
necessary to avoid cutting yourself.

--
Keith Thompson (The_Other_Keith) <kst-u@xxxxxxx>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: Alternatives to #define?
    ... >> But macros can be difficult to follow too. ... >> large parts of the code by switching a compiler flag. ... > inline void doStuff() ...
    (comp.lang.cpp)
  • Re: macros that build macros
    ... macros that built other C macros. ... The problem is that there seems to be no standard way to include a ... might have been using some sort of non-standard preprocessor trick to do it. ... Some compilers implement the preprocessor as a separate program, ...
    (comp.lang.c)
  • Re: What is the gain of "inline"
    ... Most of his arguments against inline apply to macros. ... Cons for functions: ... Code bloat ...
    (comp.lang.c)
  • Re: Alternatives to #define?
    ... > but for example logging macros and testing macros can be difficult to read ... But I do like how the compiler has an opportunity to remove ... inline void doStuff() ...
    (comp.lang.cpp)
  • [PATCH] [9/11] x86: Replace NSC/Cyrix specific chipset access macros by inlined functions.
    ... With the macros a line like this fails: ... * NSC/Cyrix CPU indexed register access macros ... static inline void sync_core ... +static inline void setCx86 ...
    (Linux-Kernel)