Advantage: Macros and Inline functions are efficient than calling a normal function. The times spend in calling the function is saved in case of macros and inline functions as these are included directly into the code.
Disadvantage: Macros and inline functions increased the size of executable code.
Submitted by Naveen Rao (naveen@voicesaver.com)
_____
Difference in inline functions and macro 1) Macro is expanded by preprocessor and inline function are expanded by compiler. 2) Expressions passed as arguments to inline functions are evaluated only once while _expression passed as argument to inline functions are evaluated more than once.
More over inline functions are used to overcome the overhead of function calls. Macros are used to maintain the readbility and easy maintainence of the code.
Submitted by Pooja Agrawal (poojas@gmail.com)
Inline Functions versus Macros
Although inline functions are similar to macros (because the function code is expanded at the point of the call at compile time), inline functions are parsed by the compiler, whereas macros are expanded by the preprocessor. As a result, there are several important differences:
*
Inline functions follow all the protocols of type safety enforced on normal functions. *
Inline functions are specified using the same syntax as any other function except that they include the inline keyword in the function declaration. *
Expressions passed as arguments to inline functions are evaluated once. In some cases, expressions passed as arguments to macros can be evaluated more than once.
macro: these get expanded during preprossing whenver invoked by the code. Disadvantage: 1. calling macro too many times in the code will increase the size of the code. 2. simple operation can be performed which does not increase the size of the code. 3. Few operations are unpredictable. example: #define SQA(x) (++x * ++x)
int main() { int a = 2; SQA(++a); } inline: processed during compilation. compiler optimizes a inline function either as a function if the performance overhead in calling a function is less compared to macro implementation, OR, it is implemented as Macro itself. This optimization makes inline more preferable than macro
Note: if function called: a. return address had to be moved to stack. b. register content has to be moved to stack. c. function arguments has to be moved to stack. d. memory allocation for the new function execution in ram has to done. e. program counter has to loaded with the new address.
Another difference is Inline functions check for the type of parameter passed, Macros don't check parameter type, you can pass strings to a macro that does some integer arithmetic. little correction in pooja answer in point 2. 2) Expressions passed as arguments to inline functions are evaluated only once while _expression passed as argument to inline functions are evaluated more than once. Expression passed to inline functions are evaluated and result is passed as value, whereas expressions passed as argument to macros are expanded, and if passed
#define MAX(a, b) ((a < b) ? b : a)
int main( void)
{
cout << "Maximum of 10 and 20 is " << MAX("20", "10") << endl;
In MACROS the code is literally copied into the location it was called from. So if the user passes a "double" instead of an "int" then problems could occur.
In INLINE functions, if this senerio happens the compiler will complain about incompatible types.
In MACRO --> you cannot make the macro return something which is not the result of the last expression invoked inside it.
In INLINE --> you can return any value by using keyword return();
In MACROS -- > debugging is tough (because they refer to the expanded code, rather than the code the programmer typed.) INLINe --> debugging is easy