当前位置:网站首页>va_ List usage summary
va_ List usage summary
2022-07-27 14:24:00 【Remember to look up at the stars】
va_list Use summary
Preface
stay Variable parameter function in , You can often see va_list、va_start、va_arg、va_end Use .
What is a variable parameter function ?
C In language ,printf function , Is a variable parameter function , The number of parameters passed in is uncertain , You can pass in more than one .
printf The function prototype :
int printf(const char *format, ...)
among ..., It stands for Variable parameters , Similar to ellipsis .
Example
Here is a simple example to experience variable parameter functions .
static void va_list_test(int param_num, ...)
{
int i = 0;
va_list ap;
va_start(ap, param_num);
// Get integer parameters
int arg1 = va_arg(ap, int);
printf("arg1 = 0x%x\n", arg1);
// Get string
char *arg2 = va_arg(ap, char *);
printf("arg2 = %s\n", arg2);
// Get integer parameters
int arg3 = va_arg(ap, int);
printf("arg3 = 0x%x\n", arg3);
va_end(ap); // Pay attention to the need for va_end(ap)
}
int main()
{
va_list_test(2, 0x55, "arg test", 0x66);
return 0;
}
The output is as follows :
arg1 = 0x55
arg2 = arg test
arg3 = 0x66
analysis
The above example , Separately introduced 4 Parameters , 2, 0x55, "arg test", 0x66, It can be downloaded from va_list_test() From the format of the parameter passed in by the function :
Parameters param_num The role of
Why do I need param_num Fixed parameters of ?
- Parameters param_num, That is, from the above example
Numbers 2, It's a fixed parameter . There is no practical use in the above example , For givingva_startPosition the variable parameters .va_start(ap, param_num)It can be understood as , By fixing parametersparam_numCome on Find the starting position of variable parameter storage , And saved it toapvariable . This with Function call procedure , The function frame is related to the stack , No discussion .
The process of obtaining parameters separately
Get the first variable parameter
- Parameters 0x55, It's an integer parameter , because
va_start(ap, param_num)Parameters passed inparam_numLocate the starting position of the variable parameter , So the first one is to get integer parameters 0x55, useva_arg(ap, int)To get the first variable parameter ,0x55, according toapThe location of , Read an integer value .
Get the second variable parameter
- Parameters "arg test", It's a string parameter , Due to reading 0x55 after , ap The position of has changed , It becomes the starting position of the second parameter , therefore
va_arg(ap, char *)Will take the current position , Get a string value , So get"arg test".
Get the third variable parameter
- Last parameter 0x66, It's the same thing , continue
va_arg(ap, int)Get one int Parameters .
A small summary
- We need to understand the function of the first fixed parameter , namely
va_start(ap, param_num), At the same time, we need to pay attention toapThe meaning of this parameter in obtaining variable parameters , It can be understood that pointer address is cheap , Throughout Point to the address of the first variable parameter that is not obtained - Experimental process , Try to set variable parameters 1、2、3 The acquisition order of changes , That is, the order of acquisition is determined by
int 、 char *、intIt is amended as followsint 、int、char *, There will be , Parameters 2 Get exception , Parameters 3 printf A segment error occurred after . therefore The acquisition order of variables cannot be wrong , Otherwise, there will be abnormalities . va_start()Andva_end()Need to pair up . Don't useva_end(ap)The consequences of , Guess it may lead to memory leakage . You can check by yourself .
va_list In a common way
In most practical cases ,va_list Are all with printf() The use of functions is similar .
For example:
int va_list_test(char *fmt, ...)
{
char out[1024] = {
0};
va_list ap;
va_start(ap, fmt);
vsnprintf(out, sizeof(out), fmt, ap);
printf("%s", out);
va_end(ap);
}
int main()
{
va_list_test("output : 0x%x, %s, 0x%x\n", 0x55, "arg test", 0x66);
return 0;
}
The output is as follows :
output : 0x55, arg test, 0x66
analysis
In the above example , Fixed parameter fmt It's actually incoming "output : 0x%x, %s, 0x%x\n", Three variable parameters , According to %x、%s、%x In the form of , adopt vsnprintf() Format input to out in . similar sprintf().
What needs attention is why it is used vsnprintf()
Let's take a look at vsnprintf Prototypes of functions , You can see the last parameter passed in , Namely va_list Variable , So use vsnprintf In the above example, it is very convenient .
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
A small summary
- Be careful vsnprintf() How to use the function .
- va_list Experience accumulation in common situations .
边栏推荐
- Interview secrets are widely distributed, and the exclusive secrets of editing, testing and learning are leaked?!
- PROFINET simulator tutorial
- Architecture - the sublimation of MVC
- this指向问题,闭包以及递归
- 基于在线问诊记录的抑郁症病患群组划分与特征分析
- This points to problems, closures, and recursion
- 次小生成树【模板】
- 进程间通信
- Detoxify! After Harbin Institute of technology was disabled MATLAB, domestic industrial software fought back domineering
- Rtl8762dk environment construction (I)
猜你喜欢
![[training day3] section [greed] [two points]](/img/4f/4130a1ade0ac0003adeddca780ff14.png)
[training day3] section [greed] [two points]

架构——MVC的升华

Windows10 installing SQL Server 2019

Dako held a meeting for the biological IPO: the annual revenue was 837million, and Wu Qingjun and his daughter were the actual controllers
![[luogu_p5431] [template] multiplicative inverse 2 [number theory]](/img/e0/a710e22e28cc1ffa23666658f9ba13.png)
[luogu_p5431] [template] multiplicative inverse 2 [number theory]

Cognition -- classic of the road to success of hardware engineers

PROFINET 模拟器使用教程

Arduino+ze08-ch2o formaldehyde module, output formaldehyde content

第3章业务功能开发(添加线索备注,自动刷新添加内容)

Chapter 3 business function development (add clues and remarks, and automatically refresh the added content)
随机推荐
@Repository详解
This points to problems, closures, and recursion
Shell编程规范与变量
YOLOX改进之一:添加CBAM、SE、ECA注意力机制
融合迁移学习与文本增强的中文成语隐喻知识识别与关联研究
基于在线问诊记录的抑郁症病患群组划分与特征分析
开源版思源怎么私有部署
阻塞队列BlockingQueue
第3章业务功能开发(添加线索备注,自动刷新添加内容)
Some key information about Max animation (shift+v)
SLAM综述阅读笔记七:Visual and Visual-Inertial SLAM: State of the Art, Classification,and Experimental 2021
Real image denoising based on multi-scale residual dense blocks and block connected cascaded u-net
Interview secrets are widely distributed, and the exclusive secrets of editing, testing and learning are leaked?!
How to return to the parent directory with commands
Chinese character style transfer --- antagonistic discriminative domain adaptation (L1)
Group division and characteristic analysis of depression patients based on online consultation records
Lighting 5g in the lighthouse factory, Ningde era is the first to explore the way made in China
Zhishang technology IPO meeting: annual revenue of 600million, book value of accounts receivable of 270Million
文献翻译__基于自适应全变差L1正则化的椒盐图像去噪
Utnet hybrid transformer for medical image segmentation