Javatpoint Logo
Javatpoint Logo

Inline function in C++

One of the key features of C++ is the inline function. Therefore, let's first examine the utilization of inline functions and their intended application. If make a function is inline, then the compiler replaces the function calling location with the definition of the inline function at compile time.

Any changes made to an inline function will require the inline function to be recompiled again because the compiler would need to replace all the code with a new code; otherwise, it will execute the old functionality.

In brief, When the program executes a function call instruction, the CPU copies the function's arguments to the stack, caches the memory address of the next instruction and then hands control to the targeted function. The CPU then performs the function's code, saves the return value in a designated memory address or register, and hands control back to the function that is called the function. This may become overhead if the function's execution time is shorter than the time required to move from the calling function to the called function (callee). The overhead of the function call is typically negligible compared to the length of time required to run large or complicated functions. The time needed to call a small, frequently used function, however, is often far longer than the time necessary to run the function's code. Due to the fact that their execution time is less than switching time, tiny functions experience this overhead. To minimize the overhead of function calls, C++ offers inline functions. When a function is invoked, it expands in line and is known as an inline function. When an inline function is invoked, its entire body of code is added or replaced at the inline function call location. At compile time, the C++ compiler makes this substitution. Efficiency may be increased by inline function if it is tiny. To the compiler, inlining is merely a request; it is not a command. The compiler may reject the inlining request. The compiler may not implement inlining in situations like these:

  1. If a function contains a loop. (for, while, do-while)
  2. if a function has static variables.
  3. Whether a function recurses.
  4. If the return statement is absent from the function body and the return type of the function is not void.
  5. Whether a function uses a goto or switch statement.

Syntax for an inline function:

Let's understand the difference between the normal function and the inline function.

Inside the main() method, when the function fun1() is called, the control is transferred to the definition of the called function. The addresses from where the function is called and the definition of the function are different. This control transfer takes a lot of time and increases the overhead.

When the inline function is encountered, then the definition of the function is copied to it. In this case, there is no control transfer which saves a lot of time and also decreases the overhead.

Let's understand through an example.

Once the compilation is done, the code would be like as shown as below:

Why do we need an inline function in C++?

The main use of the inline function in C++ is to save memory space. Whenever the function is called, then it takes a lot of time to execute the tasks, such as moving to the calling function. If the length of the function is small, then the substantial amount of execution time is spent in such overheads, and sometimes time taken required for moving to the calling function will be greater than the time taken required to execute that function.

The solution to this problem is to use macro definitions known as macros. The preprocessor macros are widely used in C, but the major drawback with the macros is that these are not normal functions which means the error checking process will not be done during the compilation.

C++ has provided one solution to this problem. In the case of function calling, the time for calling such small functions is huge, so to overcome such a problem, a new concept was introduced known as an inline function. When the function is encountered inside the main() method, it is expanded with its definition thus saving time.

We cannot provide the inlining to the functions in the following circumstances:

  • If a function is recursive.
  • If a function contains a loop like for, while, do-while loop.
  • If a function contains static variables.
  • If a function contains a switch or go to statement

When do we require an inline function?

An inline function can be used in the following scenarios:

  • An inline function can be used when the performance is required.
  • It can be used over the macros.
  • We can use the inline function outside the class so that we can hide the internal implementation of the function.

Advantages of inline function

  • In the inline function, we do not need to call a function, so it does not cause any overhead.
  • It also saves the overhead of the return statement from a function.
  • It does not require any stack on which we can push or pop the variables as it does not perform any function calling.
  • An inline function is mainly beneficial for the embedded systems as it yields less code than a normal function.

Disadvantages of inline function

The following are the disadvantages of an inline function:

  • The variables that are created inside the inline function will consume additional registers. If the variables increase, then the use of registers also increases, which may increase the overhead on register variable resource utilization. It means that when the function call is replaced with an inline function body, then the number of variables also increases, leading to an increase in the number of registers. This causes an overhead on resource utilization.
  • If we use many inline functions, then the binary executable file also becomes large.
  • The use of so many inline functions can reduce the instruction cache hit rate, reducing the speed of instruction fetch from the cache memory to that of the primary memory.
  • It also increases the compile-time overhead because whenever the changes are made inside the inline function, then the code needs to be recompiled again to reflect the changes; otherwise, it will execute the old functionality.
  • Sometimes inline functions are not useful for many embedded systems because, in some cases, the size of the embedded is considered more important than the speed.
  • It can also cause thrashing due to the increase in the size of the binary executable file. If the thrashing occurs in the memory, then it leads to the degradation in the performance of the computer.

What is wrong with the macro?

Readers who are familiar with C are aware that it uses macros. All direct macro calls within the macro code are replaced by the preprocessor. It is advised to always use inline functions rather than macros. The inventor of C++, Dr. Bjarne Stroustrup, claims that macros are rarely required in C++ and are error-prone. The use of macros in C++ is not without issues. Private class members are inaccessible to macro. Although macros resemble function calls, they do not truly do so.

Example

The C++ compiler verifies that the necessary conversions are made and that the inline functions' parameter types are valid. A preprocessor macro cannot accomplish this. Additionally, the preprocessor manages macros, while the C++ compiler manages inline functions. Keep in mind that while it is true that all functions specified inside a class are implicitly inlined and that the C++ compiler will execute inline calls to these functions, inlining cannot be performed if the function is virtual. The cause is that a virtual function's call is resolved at runtime rather than at compile time. If the compiler doesn't know which function will be called, how can it conduct inlining during compilation when virtual means waiting till runtime? Another thing to keep in mind is that moving a function intoline will only be beneficial if the time required to call the function is longer than the time required to execute the function body. As an illustration, consider the following:

Example

Executing the aforementioned function takes a while. The basic rule is that a function that performs input output (I/O) operations shouldn't be defined as inline because it takes a lot of time. Since the I/O statement would take far longer than a function call would, technically, inlining the show() function is only of limited utility.

If the function is not expanded inline, depending on the compiler you are using, a warning may be displayed. Inline functions cannot be used in programming languages like Java or C#.

But because final methods cannot be overridden by subclasses and calls to final methods are handled at compile time, the Java compiler can conduct inlining when the small final process is called. By inlining short function calls, the JIT compiler can further optimize C# code (like replacing body of a small function when it is called in a loop).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA