当前位置:网站首页>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); 
边栏推荐
- Warning in install. packages : package ‘RGtk2’ is not available for this version of R
- The mysqlbinlog command uses
- 移位运算符
- 2022.02.13 - NC002. sort
- JS pure function
- Revit 二次开发 HOF 方式调用transaction
- Light of domestic games destroyed by cracking
- Image, CV2 read the conversion and size resize change of numpy array of pictures
- 电脑F1-F12用途
- Chrome浏览器的crash问题
猜你喜欢

Deep analysis of C language pointer

Deep anatomy of C language -- C language keywords

ROS compilation calls the third-party dynamic library (xxx.so)

Leetcode question brushing (5.28) hash table

Image,cv2读取图片的numpy数组的转换和尺寸resize变化

Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures

2022.02.13 - NC002. sort

个人电脑好用必备软件(使用过)
![Verrouillage [MySQL]](/img/ce/9f8089da60d9b3a3f92a5e4eebfc13.png)
Verrouillage [MySQL]

Image, CV2 read the conversion and size resize change of numpy array of pictures
随机推荐
The network model established by torch is displayed by torch viz
Roguelike game into crack the hardest hit areas, how to break the bureau?
[MySQL] database stored procedure and storage function clearance tutorial (full version)
Detailed explanation of heap sorting
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
JS inheritance method
电脑清理,删除的系统文件
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
[NVIDIA development board] FAQ (updated from time to time)
C語言雙指針——經典題型
[cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes
2022.02.13 - 238. Maximum number of "balloons"
根据csv文件某一列字符串中某个数字排序
可变长参数
Process of obtaining the electronic version of academic qualifications of xuexin.com
Leetcode question brushing (5.28) hash table
Navicat Premium 创建MySql 创建存储过程
JVM performance tuning and practical basic theory - Part 1
TCP/IP协议
swagger设置字段required必填