当前位置:网站首页>Variable length parameter
Variable length parameter
2022-07-06 08:43:00 【Flying_】
Catalog
Why can variable length parameters be implemented ?
#include <stdio.h>
#include <stdarg.h>
void MultiArg(int prev_param, ...)
{
va_list arg_ptr;
//va_start take arg_ptr Set to the first optional parameter in the parameter list passed to this function . Parameters arg_ptr Must have va_list type
// In the first use va_arg Must be used before va_start
//va_arg from arg_ptr Retrieve... From the specified location type Value , And increase arg_ptr By using type To determine the starting position of the next parameter
// To point to the next parameter in the list
//prev_param Is the address of the fixed parameter in memory , Calling va_start after ,arg_ptr Point to the first variable parameter . The function of this macro is to prev_param Increase the memory address of prev_param The amount of memory occupied , This gives the address of the first variable parameter .
va_start(arg_ptr, prev_param);
for (int i = 0; i < prev_param; i++)
{
int value = va_arg(arg_ptr, int);
printf(" The first %d Optional parameters =%d\n", i, value);
}
// After retrieving all parameters , Reset va_end Pointer to NULL, Before the function returns, it must be used va_start or va_copy Call on the initialized parameter list va_end
va_end(arg_ptr);
}
// The last parameter needs to be -1
void MulitArg_2(int prev_param,...)
{
va_list var_ptr;
va_start(var_ptr, prev_param);
int value = 0;
int i = 0;
// Take out the optional parameters in the stack in turn
while ((value = va_arg(var_ptr, int)) != -1)
{
printf(" The first %d Optional parameters =%d\n", i++, value);
}
va_end(var_ptr);
}
int main()
{
MultiArg(5, 1, 2, 3, 4, 5);
MulitArg_2(0, 1, 2, 3, 4, 5, -1);
getchar();
return 0;
}
Why can variable length parameters be implemented ?
Determined by stack structure and function call convention
Program memory layout
Modern applications run in memory space , If it is 32 Operating system of , Yes 32 Root address line , The maximum available address space is 4G,2 Of 32 Power . Most operating systems will 4GB A part of the memory space of is appropriated to the kernel , The application can't access this memory directly , This part of memory address is called kernel space .
Windows Default high address 2GB As kernel space
Linux Default high address 1GB As kernel space
The rest 3GB or 2GB It's called user space .
The general default classification of user space is as follows :
Stack : The highest address , Usually in megabytes
Pile up : Under the stack , Generally, there are tens or hundreds of megabytes of capacity
Executable image : Store the image of the executable file in memory
Reserves : Forbidden area in memory , In most operating systems, tiny addresses are not allowed to be accessed , For example, common NULL Address
Linux Typical memory layout in the next process :
Stack
The two most important registers of the stack :ebp、esp.
ebp It is fixed in the stack , Always point to the bottom of the stack .(ebp Registers are also called frame pointers )
esp Is always changing, always pointing to the top of the stack .
esp increase , Stack space decreases ,esp Reduce , Stack space increases .
The stack used to store the maintenance information required by a function call is called stack frame, also known as activity record .
Stack frames generally include :
1、 The return address and parameters of the function
2、 Temporary variable ( Non static local variables and temporary variables automatically generated by the compiler )
3、 Context ( Registers that need to remain unchanged before and after a function call )
A common frame pointer / The activity record is as follows :
Function call conventions
The caller and callee of a function need to have a clear agreement , How the parameters are passed , How is the function name modified , Only make an appointment in advance , In order to maintain the correct call , This Convention is called function calling convention .
The realization of variable length parameters benefits from C The language defaults to cdecl Transfer the conventional right to left stack pressing transfer method
The stack structure of calling variable length parameter function is :
MulitArg_2(0, 1, 2, 3, 4, 5, -1);
边栏推荐
- 移位运算符
- UnsupportedOperationException异常
- Verrouillage [MySQL]
- marathon-envs项目环境配置(强化学习模仿参考动作)
- Promise 在uniapp的简单使用
- Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
- 生成器参数传入参数
- Bottom up - physical layer
- Light of domestic games destroyed by cracking
- 【MySQL】锁
猜你喜欢
Unified ordering background interface product description Chinese garbled
Sort according to a number in a string in a column of CSV file
sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
【ROS】usb_ Cam camera calibration
TP-LINK 企业路由器 PPTP 配置
Navicat Premium 创建MySql 创建存储过程
[cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes
Deep anatomy of C language -- C language keywords
【MySQL】鎖
swagger设置字段required必填
随机推荐
Roguelike游戏成破解重灾区,如何破局?
2022.02.13 - NC002. sort
POI add write excel file
Warning in install. packages : package ‘RGtk2’ is not available for this version of R
Trying to use is on a network resource that is unavailable
游戏解包的危害及资源加密的重要性
Bottom up - physical layer
sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
Colorlog combined with logging to print colored logs
根据csv文件某一列字符串中某个数字排序
Image, CV2 read the conversion and size resize change of numpy array of pictures
egg. JS directory structure
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
【ROS】usb_ Cam camera calibration
logback1.3. X configuration details and Practice
@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
Report on Market Research and investment prospects of China's silver powder industry (2022 Edition)
R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
Synchronized solves problems caused by sharing
Swagger setting field required is mandatory