Difference between revisions of "Inline Specifier"
Proj user8 (talk | contribs) |
Proj user8 (talk | contribs) (→Basic Example of the inline Specifier) |
||
Line 2: | Line 2: | ||
=== Basic Example of the '''inline''' Specifier === | === Basic Example of the '''inline''' Specifier === | ||
+ | In the following example there are two functions that do the same thing. Except one uses the inline specifier and the other does not. When compiled using compiler optimizations, the function with the inline specifier will not have a function call. Rather, the functions contents will be put directly in the main function of the assembly output. | ||
+ | <syntaxhighlight lang="C"> | ||
+ | #include "stdio.h" | ||
+ | void print_NON_inline() | ||
+ | { | ||
+ | printf("%d", 2); | ||
+ | } | ||
+ | |||
+ | inline void print_WITH_inline() | ||
+ | { | ||
+ | printf("%d", 3); | ||
+ | } | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | print_NON_inline(); | ||
+ | print_WITH_inline(); | ||
+ | return 1; | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | <syntaxhighlight lang="NASM"> | ||
+ | 00000000 <print_NON_inline()>: | ||
+ | _Z16print_NON_inlinev(): | ||
+ | 0: 83 ec 10 sub $0x10,%esp | ||
+ | 3: 6a 02 push $0x2 | ||
+ | 5: 68 00 00 00 00 push $0x0 6: R_386_32 .rodata.str1.1 | ||
+ | a: 6a 01 push $0x1 | ||
+ | c: e8 fc ff ff ff call d <print_NON_inline()+0xd> d: R_386_PC32 __printf_chk | ||
+ | 11: 83 c4 1c add $0x1c,%esp | ||
+ | 14: c3 ret | ||
+ | |||
+ | 00000015 <main>: | ||
+ | main(): | ||
+ | 15: 8d 4c 24 04 lea 0x4(%esp),%ecx | ||
+ | 19: 83 e4 f0 and $0xfffffff0,%esp | ||
+ | 1c: ff 71 fc pushl -0x4(%ecx) | ||
+ | 1f: 55 push %ebp | ||
+ | 20: 89 e5 mov %esp,%ebp | ||
+ | 22: 51 push %ecx | ||
+ | 23: 83 ec 04 sub $0x4,%esp | ||
+ | 26: e8 fc ff ff ff call 27 <main+0x12> 27: R_386_PC32 print_NON_inline() | ||
+ | 2b: 83 ec 04 sub $0x4,%esp | ||
+ | 2e: 6a 03 push $0x3 | ||
+ | 30: 68 00 00 00 00 push $0x0 31: R_386_32 .rodata.str1.1 | ||
+ | 35: 6a 01 push $0x1 | ||
+ | 37: e8 fc ff ff ff call 38 <main+0x23> 38: R_386_PC32 __printf_chk | ||
+ | 3c: 83 c4 10 add $0x10,%esp | ||
+ | 3f: b8 01 00 00 00 mov $0x1,%eax | ||
+ | 44: 8b 4d fc mov -0x4(%ebp),%ecx | ||
+ | 47: c9 leave | ||
+ | 48: 8d 61 fc lea -0x4(%ecx),%esp | ||
+ | 4b: c3 ret | ||
+ | </syntaxhighlight> | ||
=== '''inline''' Specifier With a Recursive Function === | === '''inline''' Specifier With a Recursive Function === |
Revision as of 00:41, 15 December 2016
inline is a specifier for a function in C/C++ that tells the compiler to replace the function call in assembly with the contents of the function. It is most often used when tying to speed up a program as it takes out the overhead of a function call. it is particularly useful with small functions that are called often. However, the specifier will also increase the size of your program as it takes away the benefit of re-usability at the assembly level.
Basic Example of the inline Specifier
In the following example there are two functions that do the same thing. Except one uses the inline specifier and the other does not. When compiled using compiler optimizations, the function with the inline specifier will not have a function call. Rather, the functions contents will be put directly in the main function of the assembly output.
#include "stdio.h"
void print_NON_inline()
{
printf("%d", 2);
}
inline void print_WITH_inline()
{
printf("%d", 3);
}
int main(void)
{
print_NON_inline();
print_WITH_inline();
return 1;
}
00000000 <print_NON_inline()>:
_Z16print_NON_inlinev():
0: 83 ec 10 sub $0x10,%esp
3: 6a 02 push $0x2
5: 68 00 00 00 00 push $0x0 6: R_386_32 .rodata.str1.1
a: 6a 01 push $0x1
c: e8 fc ff ff ff call d <print_NON_inline()+0xd> d: R_386_PC32 __printf_chk
11: 83 c4 1c add $0x1c,%esp
14: c3 ret
00000015 <main>:
main():
15: 8d 4c 24 04 lea 0x4(%esp),%ecx
19: 83 e4 f0 and $0xfffffff0,%esp
1c: ff 71 fc pushl -0x4(%ecx)
1f: 55 push %ebp
20: 89 e5 mov %esp,%ebp
22: 51 push %ecx
23: 83 ec 04 sub $0x4,%esp
26: e8 fc ff ff ff call 27 <main+0x12> 27: R_386_PC32 print_NON_inline()
2b: 83 ec 04 sub $0x4,%esp
2e: 6a 03 push $0x3
30: 68 00 00 00 00 push $0x0 31: R_386_32 .rodata.str1.1
35: 6a 01 push $0x1
37: e8 fc ff ff ff call 38 <main+0x23> 38: R_386_PC32 __printf_chk
3c: 83 c4 10 add $0x10,%esp
3f: b8 01 00 00 00 mov $0x1,%eax
44: 8b 4d fc mov -0x4(%ebp),%ecx
47: c9 leave
48: 8d 61 fc lea -0x4(%ecx),%esp
4b: c3 ret