inline Vs Macro

Discussion in 'C' started by kanaks_go4ex, Aug 18, 2008.

  1. kanaks_go4ex

    kanaks_go4ex New Member

    Joined:
    Jun 11, 2008
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Hi all,

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

    regards
    kanaks
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     
  3. aditiya

    aditiya New Member

    Joined:
    Aug 21, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  4. kanaks_go4ex

    kanaks_go4ex New Member

    Joined:
    Jun 11, 2008
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    No not for code modularity but for faster execution.

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

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

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice