Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inline function vs macro function [duplicate]

Tags:

c

macros

inline

Possible Duplicate:
Inline functions vs Preprocessor macros

I want to know the difference between the inline function and macro function.

1) is inline function is the same of macro function ?

2) I know that both are not called but they are replaced by its code in the compilation phase. is not?

3) If there is difference, Could you specify it?

like image 659
MOHAMED Avatar asked Sep 06 '25 14:09

MOHAMED


2 Answers

Inline replaces a call to a function with the body of the function, however, inline is just a request to the compiler that could be ignored (you could still pass some flags to the compiler to force inline or use always_inline attribute with gcc).

A macro on the other hand, is expanded by the preprocessor before compilation, so it's just like text substitution, also macros are not type checked, inline functions are. There's a comparison in the wiki.

For the sake of completeness, you could still have some kind of type safety with macros, using gcc's __typeof__ for example, the following generate almost identical code and both cause warnings if used with the wrong types:

#define max(a,b) \
  ({ __typeof__ (a) _a = (a); \
      __typeof__ (b) _b = (b); \
    _a > _b ? _a : _b; })

__attribute__((always_inline)) int max(int a, int b) {
   return (a > b ? a : b);
}

Note: sometimes typeless macros are just what's needed, for example, have a look at how uthash uses macros to make any C structure hashable without resorting to casts.

like image 198
iabdalkader Avatar answered Sep 09 '25 11:09

iabdalkader


1) No.

2) A Macro in C is simply text that is expanded before the compiler processes the source code. The inline keyword is used as a hint to the compiler that the function can be placed inline without the need for a call-stack to be set up.

So, for example, lets say that you got the following two snippets of code (First the macro and then the inline function):

#define MAX(x,y)     (x > y ? x : y)

and

inline int max(int x, int y) { return x > y ? x : y; }

When the preprocessor finds the following call in your code:

int highest = MAX (var_1, var_2);

it replaces it with

int highest = (var_1 > var_2 ? var_1 : var_2);

The above is what the compiler eventually gets during the compilation process, hence the snippet defined by MAX(x,y) is the replacement for MAX(var_1, var_2). When the compiler finds the function call

int highest = max (var_1, var_2);

Then the function "max" gets called. You must assume that it gets called the normal way, because the compiler is free to ignore your "inline" hint and make calls to the function go through the normal call-stack rather than simply placing the code for the function in the place it is encountered.

One last caveat with macros: because it is all text replacement and not code replacement, if you do something like this:

int highest = MAX (v1++, v2++);

the preprocessor will expand that to:

int highest = (v1++ > v2++ ? v1++ : v2++);

which is probably not what you intended.

like image 20
Lelanthran Avatar answered Sep 09 '25 10:09

Lelanthran