1

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.

Kumar2080
  • 105
  • 2
  • 13
  • This very informative answer may be useful to you: [extern inline](https://stackoverflow.com/questions/216510/extern-inline/216546#216546). – kaylum Mar 31 '16 at 05:40
  • 2
    Note: in C when you specify `inline` it is a suggestion to the compiler that is not mandatory. – David C. Rankin Mar 31 '16 at 05:43
  • @DavidC.Rankin That means, the gcc compiler will consider **inline** or not by default for c programs. – Kumar2080 Mar 31 '16 at 05:53
  • I suppose `p_fun` is not inlined because it calls `printf`, and there is so much overhead caused by `printf` that inlining `p_fun` wouldn't make any difference. – Jabberwocky Mar 31 '16 at 07:25
  • What compilation options did you use? Did you specify `-O` or `-O3` or anything similar? If not, the compiler may not have bothered to inline the functions. You could also experiment with adding a `static void s_fun(void)`, similar to your `p_fun()` but static, and call that — with optimization, it might well be inlined automatically. – Jonathan Leffler Mar 31 '16 at 16:01

0 Answers0