Skip to content

C++:inline

인라인 함수(inline function)는 여러 버전의 C와 C++ 프로그래밍 언어에서 컴파일러가 인라인 확장 수행을 요청 받는 함수이다. 다시 말해, 프로그래머는 함수를 호출하는 코드를 함수가 정의된 곳에다 생성하지 않고 컴파일러가 완전한 함수체를 함수가 호출되는 모든 장소에 삽입할 것을 요청할 수 있다.

Restrictions

인라인 함수의 제약사항에 대하여 정리한다.

  • inline 함수 내에서는 루프문 (do whie, while, for), switch, goto문을 사용할 수 없다.
  • inline 함수호출시 호출되기 전에 먼저 inline 함수가 정의되어 있어야 한다.
  • inline 함수 내에서 재귀호출을 할수 없다.
  • inline 함수는 한 수식 내에서 두 번이상 호출될수 없다.
  • 함수 포인터로 inline 함수의 주소를 취할 수 없다.
    inline 함수는 호출방식이 아니라 치환전개방식이기 때문이다.

Tip

static/inline keyword position

static이나 inline과 같은 키워드는 Prorotype, Implementation 중 어느 곳에 위치해야 하는가?

답변은 Prototype.

Assembly result

아래의 코드에서 inline(__attribute__((always_inline)))구문을 사용할 경우와 사용하지 않을 경우를 Assembly 결과로 비교한다.

__attribute__((always_inline)) int add(int i)
{
    return i + i;
}

int main()
{
    int a = add(10);
    return 0;
}

참고로 objdump를 사용하여 결과를 추출하였다.

일반

inline 사용

test.o:     file format mach-o-x86-64

Disassembly of section .text:

0000000000000000 <__Z3addi>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   89 7d fc                mov    %edi,-0x4(%rbp)
   7:   8b 7d fc                mov    -0x4(%rbp),%edi
   a:   03 7d fc                add    -0x4(%rbp),%edi
   d:   89 f8                   mov    %edi,%eax
   f:   5d                      pop    %rbp
  10:   c3                      retq   
  11:   66 66 66 66 66 66 2e    data16 data16 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
  18:   0f 1f 84 00 00 00 00 
  1f:   00 

0000000000000020 <_main>:
  20:   55                      push   %rbp
  21:   48 89 e5                mov    %rsp,%rbp
  24:   48 83 ec 10             sub    $0x10,%rsp
  28:   bf 0a 00 00 00          mov    $0xa,%edi
  2d:   c7 45 fc 00 00 00 00    movl   $0x0,-0x4(%rbp)
  34:   e8 00 00 00 00          callq  39 <_main+0x19>
  39:   31 ff                   xor    %edi,%edi
  3b:   89 45 f8                mov    %eax,-0x8(%rbp)
  3e:   89 f8                   mov    %edi,%eax
  40:   48 83 c4 10             add    $0x10,%rsp
  44:   5d                      pop    %rbp
  45:   c3                      retq   
test2.o:     file format mach-o-x86-64

Disassembly of section .text:

0000000000000000 <__Z3addi>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   89 7d fc                mov    %edi,-0x4(%rbp)
   7:   8b 7d fc                mov    -0x4(%rbp),%edi
   a:   03 7d fc                add    -0x4(%rbp),%edi
   d:   89 f8                   mov    %edi,%eax
   f:   5d                      pop    %rbp
  10:   c3                      retq   
  11:   66 66 66 66 66 66 2e    data16 data16 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
  18:   0f 1f 84 00 00 00 00 
  1f:   00 

0000000000000020 <_main>:
  20:   55                      push   %rbp
  21:   48 89 e5                mov    %rsp,%rbp
  24:   31 c0                   xor    %eax,%eax
  26:   c7 45 f8 00 00 00 00    movl   $0x0,-0x8(%rbp)
  2d:   c7 45 fc 0a 00 00 00    movl   $0xa,-0x4(%rbp)
  34:   8b 4d fc                mov    -0x4(%rbp),%ecx
  37:   03 4d fc                add    -0x4(%rbp),%ecx
  3a:   89 4d f4                mov    %ecx,-0xc(%rbp)
  3d:   5d                      pop    %rbp
  3e:   c3                      retq   

결과를 보면 알겠지만 inline시 callq명령이 빠지고 add명령이 포함되었다.

Favorite site

References


  1. Netrance.blog.naver.com_-inline_function-_asm.pdf