Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   inline Vs Macro (http://www.go4expert.com/forums/inline-vs-macro-t13036/)

kanaks_go4ex 18Aug2008 23:47

inline Vs Macro
 
Hi all,

What is the difference between the Inline specifier and Macro definition in 'C' ?
Illustration with example,helps a lot.

regards
kanaks

shabbir 19Aug2008 09:10

Re: inline Vs Macro
 
There are quite a few

1. Macros are replaced at compile time and Inline functions at runtime.
2. Macros does not type checking where as inline functions can check for parameter data type
3. Inline function may not behave inline depending on its implementation body but such occurance is not possible in Macros

I am not 100% sure with point no 1 but I am sure that the time of replacement of both are different.

aditiya 21Aug2008 13:17

Re: inline Vs Macro
 
macros are replaced with the expression at the time of compilation but the inline functions are not replaced,the benifit of inline function is that in case of inline function code executed in the same block in which this function is defined ,ctrl is not transferred from one function to another

kanaks_go4ex 24Aug2008 23:54

Re: inline Vs Macro
 
Do we use Inline/Macro both for the code modularity ?

Does inline function definition saves any memory in the system ?

When we declare any function with inline ,it executes in the same block without switching
for the functional calls.Will that block space comes under the stack memory area ?

-thanks

shabbir 25Aug2008 09:31

Re: inline Vs Macro
 
Quote:

Originally Posted by kanaks_go4ex
Do we use Inline/Macro both for the code modularity ?

No not for code modularity but for faster execution.

Quote:

Originally Posted by kanaks_go4ex
Does inline function definition saves any memory in the system ?

No they waste more memory as the same function when called multiple times reside multiple times in memory.

Quote:

Originally Posted by kanaks_go4ex
When we declare any function with inline ,it executes in the same block without switching
for the functional calls.Will that block space comes under the stack memory area ?

It is not under stack memory are but substituted as statement of the calling function.

oogabooga 27Aug2008 02:10

Re: inline Vs Macro
 
Macros are a very simple textual substitution device.
The textual substitution occurs before compile time,
whereas inline functions are inlined (if at all) at
compile time.

Inline functions have to behave just like regular functions
so their code should not be considered as executing in the
same block as the surrounding code. For instance, they cannot
access variables in the surrounding code, whereas a macro
could.

Inline functions are generally preferred over macros because
of argument type checking and separation from surrounding code.


All times are GMT +5.5. The time now is 22:51.