Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Inline Functions question

red888red888 Posts: 25Member
Learning about inline functions and very confused.

When I use an inline function am I [italic]not [/italic]pushing another activation record onto the stack? If not then how does the inline function know how to return to the function that called it? And where is it exactly if its not on the stack? When the function is copied where is it copied to, RAM? I don't understand this.

If I declare a function inline and then call it multiple times in my program then I will have multiple copies of the function code in my program- I really have no idea what this means. Where does the compiler put all of these copies when it builds the program?

Thanks for any responses.

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]Compiler simply inserts the code (copies it) into the place of a function call. A simple example:[/color]
    [code]
    int SUM (int a, int b)
    {
    return a+b;
    }
    [color=Green]//
    // Without INLINE
    //[/color]
    int main ()
    {
    int n1,n2;

    printf ("ENTER TWO INTEGER VALUES: ");
    scanf ("%d %d", &n1, &n2);
    printf ("
    SUM OF THESE VALUES: %d", [color=Red]SUM (n1, n2)[/color]);
    return 0;
    }
    [color=Green]//
    // With INLINE (a simple replacement)
    //[/color]
    int main ()
    {
    int n1,n2;

    printf ("ENTER TWO INTEGER VALUES: ");
    scanf ("%d %d", &n1, &n2);
    printf ("
    SUM OF THESE VALUES: %d", [color=Red]n1+n2[/color]);
    return 0;
    }
    [/code]
    [color=Blue]Also, [italic]inline[/italic] keyword works mostly in RELEASE build - compiler does it on RELEASE build and not all is inlined. Compiler measures the SIZE of functions and inlines only the functions, which are not large. Also, if a function is very SMALL (like SUM in my example) - you do not need to write [italic]inline[/italic] for it - it will be inlined automatically, by size. All small functions are inlined this way, just by looking at the size of a function.[/color]
  • red888red888 Posts: 25Member
    Hmm maybe my question comes from a deeper ignorance of how a program is compiled.

    If I have:

    [code]
    inline int SUM (int a, int b)
    {
    return a+b;
    }

    int main ()
    {
    int n1,n2;

    printf ("ENTER TWO INTEGER VALUES: ");
    scanf ("%d %d", &n1, &n2);
    printf ("
    SUM OF THESE VALUES: %d", SUM (n1, n2));
    return 0;
    }
    [/code]

    The compiler takes "SUM (n1, n2)" and replaces it with "n1+n2"? How is that replacement made? And how does it make my program bigger?


  • red888red888 Posts: 25Member
    Hmm maybe my question comes from a deeper ignorance of how a program is compiled.

    If I have:

    [code]
    inline int SUM (int a, int b)
    {
    return a+b;
    }

    int main ()
    {
    int n1,n2;

    printf ("ENTER TWO INTEGER VALUES: ");
    scanf ("%d %d", &n1, &n2);
    printf ("
    SUM OF THESE VALUES: %d", SUM (n1, n2));
    return 0;
    }
    [/code]

    The compiler takes "SUM (n1, n2)" and replaces it with "n1+n2"? How is that replacement made? And how does it make my program bigger?


  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]The replacement is made when compiler links all together - only at this moment it can see the function size. Then the Assembly code of the function is placed instead of a function call. When code runs - it is already been replaced. The program gets larger in the case you are using a lot of calls to SUM (inlined) function. Just one call will not make it much larger - maybe a few bytes. But with inline functions you get faster code, because there is no CALL/RETURN codes. However, modern CPUs are very fast on first 16 levels of CALL/RET instructions and if code goes deeper in calls, then it will slow down, but not in very significant way - few CPU clocks (and they come 3.2 billion per second!).[/color]
  • red888red888 Posts: 25Member
    Well that makes things a bit clearer, but it seems like I still have to study this a bit more myself to fully understand it.

    Thanks.
Sign In or Register to comment.