5

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

10
  • 8
    inline is just a suggestion. It's not a requirement. If you want to force it to inline, see: stackoverflow.com/questions/8381293/… Commented Jun 11, 2014 at 18:44
  • 4
    @Mysticial this isn't jumping to a function or anywhere useful, however. It jumps to the next line. Commented Jun 11, 2014 at 18:46
  • 3
    @JanDvorak Looks like the OP didn't turn on optimizations. Commented Jun 11, 2014 at 18:46
  • 1
    @Mysticial That can explain those useless argument moves, but where has the addition gone? Also, how did GCC produce this? Commented Jun 11, 2014 at 18:48
  • 1
    @JanDvorak lol, dead code. I guess GCC still does some things even without optimizations. Commented Jun 11, 2014 at 18:49

1 Answer 1

6
  • 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

Sign up to request clarification or add additional context in comments.

6 Comments

Where in the object file is stored where the calls are supposed to lead?
@fjardon: so how do I optimize it and force the compilation inline ?
@vikkyhacks set the -O flag to a higher value. Check the manual for your compiler.
You forgot the most important point, namely that the compiler is free to ignore any and all inline hints if it wants to.
See also objdump -dr for showing relocation entries and gcc -S to generate asm listing instead of disassembling.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.