Inline: I don't get it

This is a discussion on Inline: I don't get it within the C++ Programming forums, part of the General Programming Boards category; I haven't been able to fine a very satisfying answer to this, but, what does declaring a function as "Inline" ...

  1. #1
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361

    Inline: I don't get it

    I haven't been able to fine a very satisfying answer to this, but, what does declaring a function as "Inline" accomplish?

    Is it useful? When would I want it? I just don't get it.

    Erik

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Specifying inline for a function gives the compiler a hint that this function would be a good candidate for inlining. Basically all inlining does is place the code for the function where it is called from. This might seem silly but take the following function for example:

    Code:
     int MySquare( int num )
     {
       return num * num;
     }
    When you call this function you have to push the integer argument on the stack, perform the operation pop the return address off, blah blah, etc. This is commonly known as "function overhead". If you inlined the function you wouldnt' have to worry about that because whenever you called MySquare it would just put (num * num) right there. Think of it kind of like a macro.

    When do we need it? When a function is called frequently that has only a few lines in it. You can never inline recursive functions and there are a lot of functions that even if you specify as inline will not be inlined by the compiler. You can search google and read up on other people's explanations.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    >> You can never inline recursive functions...
    Some compilers support it

    gg

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay okay, I know some compilers support it. I shouldn't have said 'never'. I was hoping no one would call me out on that.

    gg indeed sir.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    My best code is written with the delete key.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Ummm... I hope all of this is true... I don't have my books with me...

    Epo, there is a good reason why this is difficult for you to grasp. You are studying a high-level language, and you haven't studied Assembly language, or how microprocessors execute machine code.

    Assuming that the compiler takes the "hint", or "suggestion": Inline changes the structure of the compiled machine code. It does not change what you program does. It might make your program run faster!

    Normally, the machine code for a function is stored is in it's own area of memory. (A pointer-to-a-function wiill point-to that memory.) ...Something like the way you write your C++ code, with function definitions at the end of the source file, or in their own file.

    So, when the program is running and the function is called, the program-flow has to "jump-to", (aka branch-to) the function. This is where the stack overhead comes-in. The information about where to come back to, etc. has to be saved on the stack.

    The machine code for an inline function will be copied right in-line (in sequence) with the rest of the code. Program-flow doesn't have to jump anywhere... As-if you didn't call a function at all. ...Something like if you put all of your code in main() and didn't call any other functions.

    This means that if you call your function from 10 different places in your code, the machine-code for that inline function will be repeated 10 times. Your exe file will be bigger than a non-inline version.
    Last edited by DougDbug; 06-17-2004 at 07:32 PM.

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I remember this this post.

  8. #8
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Okay, thank you all so far, it's actually making more sense

    Say I have three files, Other.h, Other.cpp, and Main.cpp

    In Other.h I place:
    Code:
    inline MySquare (int Num);
    In Other.cpp I place:
    Code:
    #include "Other.h"
    
    inline MySquare (int Num)
    {
    BLAH = Num*Num
    }
    And in Main.cpp:
    Code:
    #include "Other.h"
    
    int main (void)
    {
    int BLAH;
    MySquare(5);
    
    return 0;
    }
    Upon the ending of such a program (I kept it as simple as I could), would BLAH = 25 then?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >would BLAH = 25 then?
    No, inline functions follow scope rules so BLAH would be an undeclared identifier.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    1
    Actually the keyword inline is just a type modificator as you can use:

    Code:
    #include <stdio.h>
    inline int sum(int a,int b)
    {
        return a+b;
    }
    void main()
    {
        int a=2,b=3,c;
        c=sum(a,b);
        printf("%i\n",c);
    }
    and it will print 5 on-screen.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >#include <stdio.h>
    This is deprecated in C++, you should use <cstdio> and account for namespace std if you want printf.

    >void main()
    void main is wrong in both C and C++. It always has been. main returns an int.

    >Actually the keyword inline is just a type modificator
    Well, technically it's a function specifier.

    >and it will print 5 on-screen.
    I'm not sure I understand the point of your example.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Okay, this is a tangent that has nothing to do with the original question of this thread BUT.........what's wrong with using void main (void)? If you don't care about main's return why should you have to have a return statement that does nothing?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    hmm... how exactly does inlining a recursive function work?

    what's wrong with using void main (void)? If you don't care about main's return why should you have to have a return statement that does nothing?
    It is not standard.
    Other than that, people talk about the return value being used by the operating system etc.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    1,203
    FAQ

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >what's wrong with using void main (void)?
    You just don't know what will happen. The C++ standard simply states "It shall have a return type of int". When the standard says "shall", it means there are no exceptions and failure to meet the requirement is undefined. A program that's undefined is very Very VERY bad. Let's just say that anything is allowed to happen, including a sadistic system erasing your harddrive.

    >If you don't care about main's return why should you have to have a return statement that does nothing?
    That argument has no foundation anymore. Standard C++ allows one to omit the explicit return statement from main and a success value will be returned behind the scenes. At that point there's no reason to void your main, it doesn't save keystrokes and invokes undefined behavior. It buys you nothing.
    My best code is written with the delete key.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. When to inline your *tors
    By Angus in forum C++ Programming
    Replies: 43
    Last Post: 10-29-2008, 03:38 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  4. bit shifting
    By Nor in forum C++ Programming
    Replies: 9
    Last Post: 08-08-2003, 11:55 AM
  5. Replies: 5
    Last Post: 09-17-2001, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21