1. In learning C, it is inevitable to encounter the multi-parameter function, just c also provides a number of mechanisms: macro function
#ifdef _m_alphatypedef struct {char *a0;/* Pointer to first homed integer argument * /int offset;/* byte offset of next parameter * /} va_list;#elsetypedef char * va_list;#endif_m_alpha refers to the DEC Alpha (Alpha AXP) architecture. So in general, Va_list defines a variable as a character (the type and number of variable arguments are completely controlled by the program code, and it does not intelligently recognize pointers of the number and type of different parameters).
There are definitions of these functions in the header file Stdarg.h,
Va_list, Va_arg, Va_start, Va_end:
Variable-argument (variable parameter)
INTSIZEOF Macro , gets the type occupied by the Space Length , the minimum occupancy length is an integer multiple of int://intsizeof
#define _INTSIZEOF (N) ((sizeof (n) + sizeof (int)-1) & ~ (sizeof (int)-1)) The key-to-n is int or char, the R Esult is 4, which is just an integer multiple of the length int (& front can be 4,5,6,7).
Va_start Macro , gets the address of the first parameter of the mutable argument list (AP is a pointer of type va_list, V is the leftmost parameter of the variable parameter):
#define VA_START (AP,V) (AP = (va_list) &v + _intsizeof (v)) Cast transform (Poiner) use to get first args addr ESS (AP)
By definition (va_list) &v to get the address of the starting parameter , plus _intsizeof (v), is actually the address of the next parameter parameter, that is, the first variable parameter address.
Va_start the process of reading mutable parameters is actually in the stack, using pointers, traversing the parameter list in the stack segment, from the low address to the high address one by one to read the contents of the parameters of the process ·
In the process, the stack address is from thehigh to low scoreMatching. When executing a function, the argument list is pushed into the stack,High Address section, and then into the return address of the stack function, and then into the execution code of the stack function, the stack address is constantly decreasing. In short, the distribution of functions in the stack is: address from high to low, in order: function parameter list, function return address, function execution code snippet. In the Stack, The distribution of functions is reversed. That is, the last parameter is the highest part of the address in the list, the first parameter is the lowest part of the list address. The parameters are distributed in the stack as follows:last parameter high addressThe second-lowest parameter :... .....(va_list) &v+sizeof (args-type)first parameter, (va_list) &vfunction return address :function code Snippet low address
VA_ARG Macro , gets the current parameter of the mutable parameter , returns the specified type, and points the pointer to the next parameter (the T parameter describes the type of the current parameter ):
#define VA_ARG (Ap,t) (* (t *) (AP + = _intsizeof (t) ) -_intsizeof (t) ) ) by * To get value
Va_end Macro , empty the va_list variable parameter list:
#define VA_END (AP) (AP = (va_list) 0)//null is not valid.
2. How to use
(1) First define a va_list variable in the function, which is a pointer to the parameter; va_list ap;(2) then initializes the newly defined va_list variable with the Va_start macro; Va_start (Ap,args); args is function input args(3) then return the variable parameter with Va_arg, the second parameter of Va_arg is the type of the parameter you want to return (if the function has more than one variable parameter, call Va_arg to get each parameter in turn); Va_args (ap,int);(4) Finally, the Va_end macro is used to end the variable parameter acquisition. Va_end (AP);3.Note(1) The type and number of variable parameters are completely controlled by the program code , it is not intelligent to identify the number and type of different parameters ;(2) If we do not need one by one to explain each parameter, only need to copy the variable list to a buffer, the vsprintf function can be used ;(3) because the compiler is not strict to the prototype of the variable parameter function , it is disadvantageous to the error of programming. It is not good for us to write high-quality code; Summary:VA function implementation is easier to fix steps1:va_list ap;//initial2:va_start (Ap,type);//get address3:va_arg (ap,type);//get value4:va_end (AP);//destroythe implementation is related to the stack. Variable-parameter functions are written in C only on specific occasions and are replaced by polymorphism in OOP. try not to use the C language to realise.
C Medium Va_list,va_arg,va_start,va_end Usage