Why inline function is used in c




















When your compiler inlines a function, it actually replaces your call lines with the lines of the function. If the function itself is longer than the call, it will take up more space. Other times functions are much larger and will significantly increase the size of your code.

If the function is called multiple times, your compiler might inline multiple copies of it, wasting space. There are two main types of functions you should try to inline if possible: short functions and static functions. In the opposite direction, you should probably not inline long functions or ones that are repeated often. Short functions are the low hanging fruit of inlining.

If a function is about as many lines long as its call, you should definitely inline it. Static functions are also great to inline. They are especially great to inline when they have only one call. There are several other more complex advantages to inlining functions, but they are beyond the scope of this article. Inlining short and static functions will help your program execute operations in less time, without adding too much size to your code. In other words, one may say that such a function is expanded in-line, hence named as an inline function.

Inline functions are commonly used when the function definitions are small, and the functions are called several times in a program.

Using inline functions saves time to transfer the control of the program from the calling function to the definition of the called function. However, inlining a function is just a request or suggestion to the compiler, not any mandatory command.

It depends on the compiler whether to accept or decline this suggestion of inlining a particular function. The compiler is most likely to not consider the inlining of a function under certain circumstances that are mentioned here as follows-.

Here, the inline function add int a, int b has been defined before the main function and later been called inside the main function. When the add function is called, the code of the add function definition is placed there, i. Henceforth, it saves from the overhead of function calls and returns. The working of the inline function add a,b has been illustrated below:.

This one translation unit is said to provide the external definition. In order to avoid undefined behavior, one external definition must exist in the program if the name of the function with external linkage is used in an expression, see one definition rule.

The address of an inline function with external linkage is always the address of the external definition, but when this address is used to make a function call, it's unspecified whether the inline definition if present in the translation unit or the external definition is called.

The static objects defined within an inline definition are distinct from the static objects defined within the external definition:. A C program should not depend on whether the inline version or the external version of a function is called, otherwise the behavior is unspecified.

Create account Log in. Namespaces Page Discussion. Views View Edit History.



0コメント

  • 1000 / 1000