Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C++ inline function has call instructions?

I read that with inline functions where ever the function call is made we replace the function call with the body of the function definition.

According to the above explanation there should not be any function call when inline is user.

If that is the case Why do I see three call instructions in the assembly code ?

#include <iostream>                                                                  
                                                                                     
inline int add(int x, int y)                                                         
{                                                                                    
        return x+ y;                                                                 
}                                                                                    
                                                                                     
int main()                                                                           
{                                                                                    
        add(8,9);                                                                    
        add(20,10);                                                                  
        add(100,233);                                                                
}

meow@vikkyhacks ~/Arena/c/temp $ g++ -c a.cpp
meow@vikkyhacks ~/Arena/c/temp $ objdump -M intel -d a.o
0000000000000000 <main>:
   0:   55                      push   rbp
   1:   48 89 e5                mov    rbp,rsp
   4:   be 09 00 00 00          mov    esi,0x9
   9:   bf 08 00 00 00          mov    edi,0x8
   e:   e8 00 00 00 00          call   13 <main+0x13>
  13:   be 0a 00 00 00          mov    esi,0xa
  18:   bf 14 00 00 00          mov    edi,0x14
  1d:   e8 00 00 00 00          call   22 <main+0x22>
  22:   be e9 00 00 00          mov    esi,0xe9
  27:   bf 64 00 00 00          mov    edi,0x64
  2c:   e8 00 00 00 00          call   31 <main+0x31>
  31:   b8 00 00 00 00          mov    eax,0x0
  36:   5d                      pop    rbp
  37:   c3                      ret  

NOTE

Complete dump of the object file is here

like image 799
vikkyhacks Avatar asked Jun 11 '14 18:06

vikkyhacks


People also ask

What is inline function calls?

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.

What is the advantage of inline function in C?

Inline Function in C++ The main advantage of inline functions is that you can use them with C++ classes as well. When an instruction of a function call is encountered during the compilation of a program, its memory address is stored by the compiler.

Why inline functions are static?

Static inline functions are simple. Either a function defined with the inline function specifier is inlined at a reference, or a call is made to the actual function. The compiler can choose which to do at each reference. The compiler decides if it is profitable to inline at -xO3 and above.

Does the compiler automatically inline?

Automatic function inlining and static functions At -O2 and -O3 levels of optimization, or when --autoinline is specified, the compiler can automatically inline functions if it is practical and possible to do so, even if the functions are not declared as __inline or inline .


1 Answers

  • You did not optimize so the calls are not inlined
  • You produced an object file (not a .exe) so the calls are not resolved. What you see is a dummy call whose address will be filled by the linker
  • If you compile a full executable you will see the correct addresses for the jumps

See page 28 of: http://www.cs.princeton.edu/courses/archive/spr04/cos217/lectures/Assembler.pdf

like image 85
fjardon Avatar answered Oct 08 '22 10:10

fjardon