当前位置:网站首页>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);
边栏推荐
- Revit 二次开发 HOF 方式调用transaction
- Screenshot in win10 system, win+prtsc save location
- 广州推进儿童友好城市建设,将探索学校周边200米设安全区域
- Purpose of computer F1-F12
- Deep learning: derivation of shallow neural networks and deep neural networks
- Research Report on supply and demand and development prospects of China's high purity aluminum market (2022 Edition)
- ROS编译 调用第三方动态库(xxx.so)
- Deep analysis of C language data storage in memory
- China dihydrolaurenol market forecast and investment strategy report (2022 Edition)
- Deep anatomy of C language -- C language keywords
猜你喜欢
JVM performance tuning and practical basic theory - Part 1
IOT -- interpreting the four tier architecture of the Internet of things
What is CSRF (Cross Site Request Forgery)?
堆排序详解
marathon-envs项目环境配置(强化学习模仿参考动作)
UnsupportedOperationException异常
【MySQL】锁
After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
The harm of game unpacking and the importance of resource encryption
View computer devices in LAN
随机推荐
2022.02.13 - NC004. Print number of loops
延迟初始化和密封类
【MySQL】锁
自动化测试框架有什么作用?上海专业第三方软件测试公司安利
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
sys.argv
Bitwise logical operator
Colorlog结合logging打印有颜色的日志
On the inverse order problem of 01 knapsack problem in one-dimensional state
Beijing invitation media
2022.02.13 - NC001. Reverse linked list
How to conduct interface test? What are the precautions? Nanny level interpretation
C语言双指针——经典题型
【MySQL】鎖
Rviz仿真时遇到机器人瞬间回到世界坐标原点的问题及可能原因
Deep analysis of C language pointer
egg. JS directory structure
【嵌入式】使用JLINK RTT打印log
China polyether amine Market Forecast and investment strategy report (2022 Edition)
按位逻辑运算符