当前位置:网站首页>Keyword inline (inline function) usage analysis [C language]
Keyword inline (inline function) usage analysis [C language]
2022-07-06 11:58:00 【Weiyuan escort agency】
One 、 What is an inline function
stay C In language , If some functions are called frequently , There are functions on the stack , Function stack , It will cause a large consumption of stack space or stack memory .
To solve this problem , In particular, the introduction of inline Modifier , Expressed as an inline function .
Stack space refers to the local data of the program, that is, the memory space of the data in the function , Under the system , Stack space is limited , If it is used frequently, it will cause the problem of program error caused by insufficient stack space , The end result of the function's dead loop recursive call is that the stack memory space is exhausted .
Let's take an example :
#include <stdio.h>
// The function is defined as inline namely : Inline function
inline char* dbtest(int a)
{
return (a % 2 > 0) ? " p. " : " accidentally ";
}
int main()
{
int i = 0;
for (i=1; i < 100; i++)
{
printf("i:%d Parity :%s \n", i, dbtest(i));
}
}
The above example is the use of the standard inline function , Use inline We can't see the benefits of decoration on the surface , In fact, the internal work is in every for Any call inside the loop dbtest(i) All the places in the hotel have been changed to (i%2>0)?" p. ":" accidentally " In this way, we can avoid the consumption of repeatedly opening stack memory by calling functions frequently .
In fact, this kind of problem is a bit similar to the dynamic library and static library we learned earlier , send dbtest The code in the function is put directly into main Function , perform for loop , Will keep calling this code , Instead of constantly opening up a function stack .
Two 、 The programming style of inline functions
1、 keyword inline Must be placed with the function definition body to make the function inline , Only will inline Putting it before a function declaration doesn't work .
The following style of function Foo Can't be an inline function :
inline void Foo(int x, int y); // inline Only with function declaration
void Foo(int x, int y)
{
}
And the following style of function Foo It becomes an inline function :
void Foo(int x, int y);
inline void Foo(int x, int y) // inline Together with the function definition body
{
}
So ,inline It's a kind of “ Keywords for implementation ” , Not a kind of “ Keywords for declaration ”. In a general way , The user can read the function declaration , But you don't see the definition of a function . Although in most textbooks the declaration of inline functions 、 The definition body is prefixed with inline keyword , But I think inline It should not appear in the declaration of a function . This detail does not affect the function of the function , But it shows high quality C++/C A basic principle of programming style : Declaration and definition should not be confused , Users don't have to 、 You shouldn't know if a function needs to be inlined .
2、inline There are limits to the use of
inline It is only suitable for functions with simple code in the body of culvert numbers , Cannot contain complex structure control statements such as while、switch, And the inline function itself cannot be a direct recursive function ( I also call my own functions inside ).
3、 ... and 、 Be careful with inline
Inline can improve the efficiency of function execution , Why not define all functions as inline functions ? If all functions are inline functions , You can use it “ inline ” This keyword ?
Inline is code bloat ( Copy ) At the cost of , It only saves the cost of function call , So as to improve the efficiency of function execution . If the execution time of the code in the function body , Compared with the cost of function call, it is more expensive , Then the efficient income
Little success . On the other hand , Every call to an inline function copies the code , Will increase the total code amount of the program , Consume more memory space .
Inline should not be used in the following situations :
(1) If the code in the function body is long , Using inline will result in high memory consumption cost .
(2) If there's a loop in the body of the function , Then it takes more time to execute the code inside the function than to call the function .
A good compiler will be based on the definition body of a function , Automatically cancel unworthy inlining ( This further explains inline It should not appear in the declaration of a function ).
summary :
therefore , It is appropriate to implement inline functions in header files , Save you the trouble of implementing it once per file . So the declaration should be consistent with the definition , In fact, it means , If the inline function is implemented once in every file , that , It's better to make sure that every definition is the same , otherwise , Will cause undefined behavior , That is , If the definition is not the same in every file , that , Which one does the compiler expand , It depends on the compiler . therefore , It's better to put the inline function definition in the header file .
边栏推荐
- Togglebutton realizes the effect of switching lights
- 5G工作原理详解(解释&图解)
- Detailed explanation of nodejs
- [Blue Bridge Cup 2017 preliminary] grid division
- There are three iPhone se 2022 models in the Eurasian Economic Commission database
- Vert. x: A simple login access demo (simple use of router)
- 分布式事务的实现方案
- Stage 4 MySQL database
- MySQL START SLAVE Syntax
- Pytoch Foundation
猜你喜欢
Vert. x: A simple login access demo (simple use of router)
ToggleButton实现一个开关灯的效果
MySQL数据库面试题
Togglebutton realizes the effect of switching lights
Pytoch Foundation
RT-Thread的main线程“卡死”的一种可能原因及解决方案
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
B tree and b+ tree of MySQL index implementation
mysql实现读写分离
Reno7 60W超级闪充充电架构
随机推荐
I2C总线时序详解
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
Encodermappreduce notes
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
R & D thinking 01 ----- classic of embedded intelligent product development process
SQL time injection
Correspondence between STM32 model and contex M
[Blue Bridge Cup 2017 preliminary] grid division
arduino获取数组的长度
TypeScript
FTP file upload file implementation, regularly scan folders to upload files in the specified format to the server, C language to realize FTP file upload details and code case implementation
2019 Tencent summer intern formal written examination
[NPUCTF2020]ReadlezPHP
【presto】presto 参数配置优化
Small L's test paper
Wangeditor rich text component - copy available
STM32 如何定位导致发生 hard fault 的代码段
Pytoch Foundation
MySQL主从复制的原理以及实现
共用体(union)详解【C语言】