I'm trying to understand how the inline functions are linked at linker.
here is the code snippet, how i am trying to check.
inline.c
#include<stdio.h>
#include"inline.h"
extern inline void in_fun();
void p_fun(void)
{
printf("in %s\n",__func__);
}
int main()
{
int a=5;
printf("main: a=%d at %p\n",a,&a);
in_fun();
printf("main: a=%d at %p\n",a,&a);
p_fun();
}
inline.h
inline void in_fun(void)
{
int a=10;
printf("inline: a=%d at %p\n",a,&a);
}
From wikipedia i got the inline function definition
In the C and C++ programming languages, an inline function is one qualified with the keyword inline; this serves two purposes. Firstly, it serves as a compiler directive that suggests (but does not require) that the compiler substitute the body of the function inline by performing inline expansion, i.e. by inserting the function code at the address of each function call, thereby saving the overhead of a function call.
so from the above code i made the disassemble of main, there for inline function it is also getting called with call instruction & for normal function is also getting called with call instruction.
here is the disassembled part of main function
0x0804840b <+0>: push ebp
0x0804840c <+1>: mov ebp,esp
0x0804840e <+3>: and esp,0xfffffff0
0x08048411 <+6>: sub esp,0x20
0x08048414 <+9>: mov DWORD PTR [esp+0x1c],0x5
0x0804841c <+17>: mov edx,DWORD PTR [esp+0x1c]
0x08048420 <+21>: mov eax,0x804854f
0x08048425 <+26>: lea ecx,[esp+0x1c]
0x08048429 <+30>: mov DWORD PTR [esp+0x8],ecx
0x0804842d <+34>: mov DWORD PTR [esp+0x4],edx
0x08048431 <+38>: mov DWORD PTR [esp],eax
0x08048434 <+41>: call 0x80482f4 <printf@plt>
0x08048439 <+46>: call 0x80483c4 <in_fun>
0x0804843e <+51>: mov edx,DWORD PTR [esp+0x1c]
0x08048442 <+55>: mov eax,0x804854f
0x08048447 <+60>: lea ecx,[esp+0x1c]
0x0804844b <+64>: mov DWORD PTR [esp+0x8],ecx
0x0804844f <+68>: mov DWORD PTR [esp+0x4],edx
0x08048453 <+72>: mov DWORD PTR [esp],eax
0x08048456 <+75>: call 0x80482f4 <printf@plt>
0x0804845b <+80>: call 0x80483ee <p_fun>
0x08048460 <+85>: leave
0x08048461 <+86>: ret
so i want to know how inserting the function code at the address of each function call is justified & how fastly it is executing & why the inline functions are written in header files only.
can any one can explain this.