3

I am fairly new to C and C++ and I am trying to understand about functions. I came across this term called inline function and understand it as when a function is declared inline, the compiler pastes the entire code in that function whenever and wherever it is called.

I thought this is actually what happens at a function call but now realize that is not the case.

Can someone explain in detail as to what happens at the compiler and system level when a normal function is called and an inline function is called?

Any material on understanding this will be appreciated.

timrau
  • 22,578
  • 4
  • 51
  • 64
user3435894
  • 97
  • 2
  • 6
  • I'd recommend to write a very simple C program which calls a function (make it just a one-liner), compile it with optimization turned off and look at the resulting machine code with a program called a disassembler. I'm sure you can do it in VS express (but don't know how). If you can handle a text console you can install cygwin with gcc and binutils. You can produce an executable or just an object file e.g. with `gcc -O0 -g -c myprog.c`. (-g includes debugging infos, -O0 means "do not optimize"). Then look at the diassembly with "objdump -dS | less" and search for a variable name. – Peter - Reinstate Monica Apr 21 '14 at 05:10
  • Note: Modern compilers can sometimes ignore `inline` and decide by themselves whether to inline functions. – user253751 Apr 21 '14 at 08:11

4 Answers4

2

When calling a (non-inline) function, the compiler has to place the function parameters/arguments in a location where the called function will expect to find them. In some cases, it will 'push' the arguments onto the process/thread's stack. In other cases, CPU registers might be assigned to specific arguments. Then, the "return address", or the address following the called function is pushed on the stack so that the called function will know how to return control back to the caller.


When calling an inline function, the compiler simply weaves the function into the code. There is no need for a common protocol between the caller, and called, functions as to where parameters will be placed. A 'return' statement (in the called in-line function), is generally implemented (by the compiler) to jump to the next instruction following the inline code.


An inline function, if called many times in the code, will cause the code size to increase. However, it is generally less expensive (in cpu cycles) to make an inline-call, than to make a function call.

MD XF
  • 7,860
  • 7
  • 40
  • 71
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
  • 2
    The `inline` keyword has nothing to do with a particular implementation strategy. Its semantics are actually related to the one-definition-rule. (Specifically, you should mark a function `inline` if you would otherwise violate the one definition rule by having multiple translation units contain definitions of that function). See [this answer](http://stackoverflow.com/a/1759575/485561). – Mankarse Apr 21 '14 at 05:05
1

When a program calls a function, program control is transferred to the called function. A called function performs defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.

Chaithra
  • 1,130
  • 3
  • 14
  • 22
0

Try this https://softwareengineering.stackexchange.com/questions/195385/understanding-stack-frame-of-function-call-in-c-c -- for a normal function working behaviour

For inline functions how Inline function code is replaced in calling place?

Community
  • 1
  • 1
Gibbs
  • 21,904
  • 13
  • 74
  • 138
0

Function calls are not free, especially in perf critic sections(eg event loop) . Old stack adress, new function parameters,temprories and return vals. are being pushed to stack when you make func. call which may result you losing some clocks from CPU.

Except it is not very critical part of code don't worry about it compiler will inline it if it can . But if it is a part of code which runs so often(like while(true) loop something) you may try force your compile to copy-paste the code without func. call by indicating inline in the beginning of the func.

Last words if you try to inline all funcs. you may end up having cache misses etc.. and worse perf. Let compiler optimize it for you except fatal parts of code.

Kadir Erdem Demir
  • 3,531
  • 3
  • 28
  • 39