当前位置:网站首页>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);
边栏推荐
- R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
- China Light conveyor belt in-depth research and investment strategy report (2022 Edition)
- China dihydrolaurenol market forecast and investment strategy report (2022 Edition)
- JVM performance tuning and practical basic theory - Part 1
- visdom可视化实现与检查介绍
- Revit 二次开发 HOF 方式调用transaction
- The network model established by torch is displayed by torch viz
- Roguelike game into crack the hardest hit areas, how to break the bureau?
- C语言深度解剖——C语言关键字
- 从表中名称映射关系修改视频名称
猜你喜欢
MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
C语言双指针——经典题型
Double pointeur en langage C - - modèle classique
Pointer advanced --- pointer array, array pointer
Beijing invitation media
ESP8266-RTOS物联网开发
ROS编译 调用第三方动态库(xxx.so)
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
sublime text中conda环境中plt.show无法弹出显示图片的问题
Detailed explanation of heap sorting
随机推荐
MySQL learning record 10getting started with JDBC
sublime text的编写程序时的Tab和空格缩进问题
角色动画(Character Animation)的现状与趋势
[MySQL] log
【刷题】牛客网面试必刷TOP101
Function coritization
Leetcode question brushing (5.31) string
Crash problem of Chrome browser
【MySQL】鎖
MySQL learning records 12jdbc operation transactions
win10系统中的截图,win+prtSc保存位置
CISP-PTE实操练习讲解
Problems in loading and saving pytorch trained models
Restful API design specification
如何有效地进行自动化测试?
Chrome浏览器的crash问题
企微服务商平台收费接口对接教程
Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
Hutool gracefully parses URL links and obtains parameters
Swagger setting field required is mandatory