What is the purpose of using inline functions in the C language?

Asked

Viewed 3,570 times

7

I would like to know what is the purpose of functions inline in the language C? Is there any difference in performance or other characteristics that should be taken into account in comparison to functions common?

Example of a function inline:

inline void funcao_inline_mensagem()
{
    printf("\nStack OverFlow\nFunção inline.\n\n");
}

Example of a function common:

void funcao_comun_mensagem()
{
    printf("\nStack OverFlow\nFunção comum.\n\n");
}

Complete implementation of the two examples to be reproduced:

#include <stdio.h>

inline void funcao_inline_mensagem()
{
    printf("\nStack OverFlow\nFunção inline.\n\n");
}

void funcao_comun_mensagem()
{
    printf("\nStack OverFlow\nFunção comum.\n\n");
}

int main(void)
{
    funcao_comun_mensagem();

    funcao_inline_mensagem();

    return 0;
}

1 answer

10


Any decent compiler in C today tries to force the functions to be inline whenever it is worthwhile to do this, regardless of the code given. Of course it is possible to control this via switch compilation.

The optimization of inline puts the body of the function at the location of its call avoiding having to prepare the environment for the new function - usually save registers and copy arguments - and restore at its completion (prologue and epilogue of function). This makes the code faster under certain circumstances. Optimization can not always reduce code so much, especially it can be difficult to avoid data copies.

It only pays to do it in relatively small jobs and they will perform very quickly. These functions can have a good part of the execution time formalizing the function and not processing what is desired. Functions with multiple instructions and mainly with loop does not usually compensate.

The compiler is usually very smart about this, it’s rare for the programmer to know when to do it inline better than the compiler knows.

There are cases that this optimization enables other possible optimizations, as well as other optimization makes the code suitable for inline.

If the programmer forces where he should not, not only can he lose performance, mainly by filling the cache more easily, but he may also have some problems not always easy to notice.

If the programmer knows how to measure (profile) the application and determine that there really will be gain where the compiler can not perceive, after all this is not easy task, then it may be interesting to force manually.

At some point it was included in the possible syntax to determine whether the code should do it, but compilers don’t usually care much about it, some completely ignore it and decide on their own, others give more weight when the syntax is used, but it doesn’t guarantee anything. Of course a compiler can do what the programmer asked. Some even have another syntax and/or switch that force the use of this optimization.

In your example would probably optimize like this:

int main(void) {
    printf("\nStack OverFlow\nFunção inline.\n\n");
    printf("\nStack OverFlow\nFunção comum.\n\n");
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

In the past people used macros to do this. Today it is completely unnecessary in modern compilers. It is much easier to make mistakes with macros. But it is possible to make them also when trying to force the inline. In C there is still the culture of macros use. In C++, no.

The inline along with static or extern may be more useful and give a relevant instruction to the compiler.

  • Interestingly, some compilers provide a parameter to warn when an inline has been ignored (-Winline in GCC).

  • 1

    Probably, the compiler also changes, in optimization, the printf("...\n") for puts("...").

Browser other questions tagged

You are not signed in. Login or sign up in order to post.