Search

7.5 — Inline functions

The use of functions provides many benefits, including:

  • The code inside the function can be reused.
  • It is much easier to change or update the code in a function (which needs to be done once) than for every in-place instance. Duplicate code is a recipe for disaster.
  • It makes your code easier to read and understand, as you do not have to know how a function is implemented to understand what it does.
  • The function provides type checking.

However, one major downside of functions is that every time a function is called, there is a certain amount of performance overhead that occurs. This is because the CPU must store the address of the current instruction it is executing (so it knows where to return to later) along with other registers, all the function parameters must be created and assigned values, and the program has to branch to a new location. Code written in-place is significantly faster.

For functions that are large and/or perform complex tasks, the overhead of the function call is usually insignificant compared to the amount of time the function takes to run. However, for small, commonly-used functions, the time needed to make the function call is often a lot more than the time needed to actually execute the function’s code. This can result in a substantial performance penalty.

C++ offers a way to combine the advantages of functions with the speed of code written in-place: inline functions. The inline keyword is used to request that the compiler treat your function as an inline function. When the compiler compiles your code, all inline functions are expanded in-place -- that is, the function call is replaced with a copy of the contents of the function itself, which removes the function call overhead! The downside is that because the inline function is expanded in-place for every function call, this can make your compiled code quite a bit larger, especially if the inline function is long and/or there are many calls to the inline function.

Consider the following snippet:

This program calls function min() twice, incurring the function call overhead penalty twice. Because min() is such a short function, it is the perfect candidate for inlining:

Now when the program compiles main(), it will create machine code as if main() had been written like this:

This will execute quite a bit faster, at the cost of the compiled code being slightly larger.

Because of the potential for code bloat, inlining a function is best suited to short functions (eg. no more than a few lines) that are typically called inside loops and do not branch. Also note that the inline keyword is only a recommendation -- the compiler is free to ignore your request to inline a function. This is likely to be the result if you try to inline a lengthy function!

Finally, modern compilers have gotten really good at inlining functions automatically -- better than humans in most cases. Even if you don’t mark a function as inline, the compiler will inline functions that it believes will result in performance increases. Thus, in most cases, there isn’t a specific need to use the inline keyword. Let the compiler handle inlining functions for you.

Rule: Be aware of inline functions, but modern compilers should inline functions for you as appropriate, so there isn’t a need to use the keyword.

7.6 -- Function overloading
Index
7.4a -- Returning values by value, reference, and address

37 comments to 7.5 — Inline functions

  • Zafer

    What happens when the compiled code becomes large by using the inline keyword? Why is it disadvantageous to have a large compile code?

    • Well, it takes up more space on disk, for one. It can also have a negative impact on how quickly your program executes due to the way the operating system pages stuff in and out of memory. Bloated code means more page loads, which adds overhead.

      • Zafer

        I don’t know what a page load means. Is this a different type of overhead because normally inline functions are meant to reduce overhead as explained in the text.

        • Page loading is actually an operating system concept and has nothing to do with C++ itself. It’s overhead that comes from the operating system itself due to the way memory is set up.

          Your computer has a certain amount of physical memory on it. This memory needs to hold all the programs that are running. However, it’s sometimes the case that our programs want to use more memory than we have physical memory on the machine! In the old days, you’d probably get a memory error and your program wouldn’t run until you freed some stuff up. However, most modern operating systems use a system called virtual memory. Virtual memory is basically a technique that lets the operating system assign more memory to programs than it actually physically has!

          The way this works is all of the physical memory in the machine is divided into chunks called pages. When a program is loaded, the operating system load it into how ever many pages it needs. However, if we run out of free pages, the operating system will make room by temporarily writing out some older pages to disk. This is what a swap drive is for. Then, when those old pages become needed again, it reads them back into memory from disk (and possibly writes out something else)

          If you’ve ever run Windows XP (or Vista) on 512 megs of ram (or less), you know how slow it can be. Why? Because the operating system is writing pieces of itself out to disk and reading them back in all the time.

          Bloated programs tend to cause the operating system to have to read in and write out more pages (for a variety of reasons). Since writing pages to disk is extremely slow, this is something you want to avoid as much as possible.

          • Zafer

            Ok then can we say that the inline keyword speeds up the execution only when the function is not too large otherwise it can cause the program to be even slower?

            • Abusing the inline keyword could, in theory, cause the program to be even slower than usual. However, there is no guarantee that this is strictly true, as there are other factors that come into play. For example: how many times the inlined function is used, spatial access issues (the pattern in which the code is accessed), as well as whether the compiler even pays attention to the inline request (which it is free to ignore).

              As noted in the lesson text, inline is best used for functions that are no more than a couple of lines.

  • Daniel

    Wow! These tutorials are growing almost as fast as I can read them 🙂

    Could you please explain the major differences between using an inline function and a #define macro?

  • Thanks,that easy to understand ..:)

  • kamal

    Is it advisable to make this function as inline? (It has loop which iterates for large number of times)

    • I wouldn’t -- I only use inlines for functions that are absolutely trivial. Anything with a loop doesn’t qualify. I’m pretty sure if you put a loop in an inline function the compiler will just treat it as a normal function.

      • pravin

        how would i know whether for above (for loop ) function, the compiler had accepted/rejected our request
        of making the function inline..

        the tutorials are very good..

        • Len

          I wondered this as well.  There is a Codeblocks compiler option "Warn if a function can not be inlined and it was declared as inline"

          You waited 6 years for an answer, you finally got one…

  • DARK_BYTE

    Wow c++ is a lot more interesting and a lot harder than python I have to say!
    😛

  • Jay

    Hi Alex,

    One quick question. When a compiler is smart enough to decide whether “inline” is applicable or not (based on some hidden criteria by compiler as you explained above), as a common practice why can’t we define each and every function as an inline function and leave it up to the compiler to decide whether to treat it as an inline or not. what could be the ups and downs by following such a style of coding?

    • Sajjad

      i have the same question.

      • ellankavi

        Well, I was thinking if the compiler is smart enough, why can’t it do this work implicitly… But, I too would like to know the answer to Jay’s question…

    • Joe

      I don’t think compilers are smart enough to know whats best. It only knows very specific situations, where its definitely not efficient and will ignore inline under those conditions. but it certainly isn’t smart enough to make every decision for you.

    • Alex

      You could, but the compiler will not only ignore inline requests you’ve made that it doesn’t think should be inlined, it will also inline functions you haven’t marked as inline that it thinks should be inlined. So the inline keyword is just a hint that the compiler is free to ignore or add as it pleases.

      As a result, there’s generally no need to inline functions yourself. If you’re optimizing your code, you could try inlining functions to see if there’s a performance improvement, but outside of that and a few other obscure edge cases, you’re better off not cluttering your code up with uses of the inline keyword.

  • dice3000

    I used to thing that this was done by default to all functions. Now I don’t 🙂

  • Ashu

    I have Little doubt regarding Inline function:

    void main()
    {
    int i=2;
    fun();
    printf(“%d”,i);
    }

    inline void fun()
    {
    int i=4;
    printf(“%d”, i);
    }

    As compiler make copy of this fun() function and insert inside main function body. why compiler not give error of variable int i re declaration.

    • Marc

      I am pretty sure that the inline function has it’s own scope and the compiler would automatically use the local i.

      • Alex

        The compiler has to be smart about how it inlines code. As Marc suggests, it can put the function code in its own block to avoid naming collisions with already declared variables. It also needs to know how to convert parameters to arguments, which can be tricky since arguments passed by value and reference are handled differently.

  • Sasan

    Does Inline functions effect/differ in the release version of program? (.exe for example)

    • Alex

      Not to my knowledge. An inline function should work the same way in release or debug.

      That said, your compiler may set different defaults for whether (or how aggressively) it inlines functions. So some functions may be inlined in the release build and not in the debug build.

  • c++ freak

    i think lot of things are missing here … your lesson are making us to be more confused than we were.. it will be good if u post a program using inline function..
    people are getting confused here regrading what type of functions when made inline are ignored by the compiler.. why does compiler ignores them …
    … please reply to above comments ..
    anyways nice tutorials

    • Jacob Perkins

      You’re confusing two important terms:  the c++ standard [vs] the c++ implementation.  It is up to the c++ implementation to decide what should be inlined and what shouldn’t.  Various C++ compilers like gcc or the visual studio c++ compiler are just different implementations of the c++ standard.  How different compilers treat your code will vary from one compiler to another because obviously they are not all built the same way.

  • Michael

    Hello Alex!

    Thanks again for your excellent tutorials!

    I have a question about inline functions and multiple files.
    I have a project that has main.cpp functions.cpp and header.h - to keep things organized.

    I have this small function stored in functions.cpp:

    in header.h I have its declaration:

    char yourChoice(std::string cstMessage); //simple char return function (no Enter key)

    and I #include "header.h" in both main.cpp and functions.cpp

    I call this function from main.cpp

    Everything was working great until I decided to inline this function.

    I get an error "main.cpp|87|undefined reference to `yourChoice(std::string)"

    All I did was add ‘inline’ in both the header and function.cpp file, like this:

    However, when I copied this function from functions.cpp to main.cpp and put it before my int main(){} and added that ‘inline’ as above - everything compiled smoothly just like before inlinig it.

    Am I doing something wrong?

    • Alex

      An inline function needs to have a definition in every place it gets used. You got an error because inline yourChoice() wasn’t defined in function main() where you were using it. This wasn’t a problem when it was non-inline because C++ can resolve the dependency, but inline functions have different rules.

  • malt

    Little question:

    inline functions == closures == lambda functions ?

    Great tuto by the way!

  • Chris

    Is it possible to create a function pointer to an inline function such that every time the function pointer is called, a the function that it points to will be compiled into your code in place.

  • Nyap

    Does the program get moved to memory when it’s ran? I heard that happens on linux which is why you can delete a program and continue to use it until you close it but didn’t know that happens everywhere

    • Alex

      I don’t think modern OSes typically load the entire executable into memory at launch, just parts of it, with the rest loaded on demand. The OS handles all of loading/memory management stuff for you, so it’s not typically exposed.

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter