当前位置:网站首页>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 .
边栏推荐
猜你喜欢
![[Blue Bridge Cup 2017 preliminary] grid division](/img/e9/e49556d0867840148a60ff4906f78e.png)
[Blue Bridge Cup 2017 preliminary] grid division

数据分析之缺失值填充(重点讲解多重插值法Miceforest)

锂电池基础知识

Variable star user module

Unit test - unittest framework

Gallery's image browsing and component learning

高通&MTK&麒麟 手机平台USB3.0方案对比

Apprentissage automatique - - régression linéaire (sklearn)

【yarn】Yarn container 日志清理

分布式節點免密登錄
随机推荐
4、安装部署Spark(Spark on Yarn模式)
使用LinkedHashMap实现一个LRU算法的缓存
Funny cartoon: Programmer's logic
Gallery之图片浏览、组件学习
Togglebutton realizes the effect of switching lights
Unit test - unittest framework
[NPUCTF2020]ReadlezPHP
分布式節點免密登錄
4. Install and deploy spark (spark on Yan mode)
关键字 inline (内联函数)用法解析【C语言】
Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
Word排版(小计)
【flink】flink学习
機器學習--線性回歸(sklearn)
B tree and b+ tree of MySQL index implementation
选择法排序与冒泡法排序【C语言】
E-commerce data analysis -- User Behavior Analysis
【kerberos】深入理解kerberos票据生命周期
数据库面试常问的一些概念
Vert. x: A simple login access demo (simple use of router)