Document directory
- I. Basics
- Ii. In-depth principles
- Iii. Knowledge Extension
- References
Independent blog link: http://www.keepsimply.org/2012/08/18/deep-explore-c-va-arg/
Author: drinking and drinking
Time: 2012.08.18
I. Basic part 1.1 what is a variable length parameter
Variable Length Parameter: As the name implies, the parameter length (quantity) of the function is variable. For example, the C-language printf series (formatted input and output) functions are variable parameters. The Declaration of the printf function is as follows:
Int printf (const char * format ,...);
Variable Parameter Function declaration is similar.
1.2 How to Implement
Variable parameters in C language are implemented through three macros (va_start, va_end, va_arg) and one type (va_list,
Void va_start (va_list AP, paramn );
Parameters:
AP: Address of the Variable Parameter List
Paramn: the specified parameter.
Function: Initialize the variable parameter list (put the parameter address of the function after paramn into the AP ).
Void va_end (va_list AP );
Function: Disable the initialization list (leave the AP empty ).
Type va_arg (va_list AP, type );
Function: return the value of the next parameter.
Va_list: stores the parameter type information.
Well, considering the three macros and one type above, we can guess how to implement the Variable Length Parameter Function in C language:Use va_start to obtain the parameter list (address) and store it in the AP. Use va_arg to obtain values one by one. Finally, use va_arg to empty the AP.
1.3 example
/* Author: discretion * Time: 2012.08.18 * function: using C language to implement variable-length parameters. Example: Sum * ide: Microsoft Visual Studio 2010 */# include <stdio. h> # include <stdarg. h> # define end-1int va_sum (INT first_num ,...) {// (1) define the parameter list va_list AP; // (2) initialize the parameter list va_start (AP, first_num); int result = first_num; int temp = 0; // get the parameter value while (temp = va_arg (AP, INT ))! = END) {result + = temp;} // close the parameter list va_end (AP); return result;} int main () {int sum_val = va_sum (1, 2, 3, 4, 5, end); printf ("% d", sum_val); Return 0 ;}
1.4 precautions
- The macro is defined in stdarg. H, so do not forget to add the header file when using it.
- Set a parameter end flag (in cplusplus, va_arg cannot determine which parameter is the last parameter ).
- Type Matching
- We look forward to hearing from you ......
Ii. In-depth principles
"In front of the source code, a clear view "!
The following source code is from ".. \ Microsoft Visual Studio 10.0 \ Vc \ include"
// stdarg.h#define va_start _crt_va_start#define va_arg _crt_va_arg#define va_end _crt_va_end// vadefs.htypedef char * va_list;#define _crt_va_start(ap,v) ( ap = (va_list)_ADDRESSOF(v) + _INTSIZEOF(v) )#define _crt_va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )#define _crt_va_end(ap) ( ap = (va_list)0 )#define _ADDRESSOF(v) ( &(v) )#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
Except for _ intsizeof, others are well understood. For example:
/* Author: discretion * Time: 2012.08.18 * function: test_intsizeof macro * ide: codeblocks 10.05 */# include <stdio. h> # include <stdarg. h> int main () {int I = 1; float F = 0.0; printf ("_ intsizeof (I) = % d \ n", (INT) (_ intsizeof (I); printf ("_ intsizeof (f) = % d \ n", (INT) (_ intsizeof (F ))); printf ("_ intsizeof (\" Hello, world \ ") = % d \ n", (INT) (_ intsizeof ("Hello, world "))); printf ("sizeof (\" Hello, world \ ") = % d \ n", sizeof ("Hello, world"); Return 0 ;}
Output result:
_INTSIZEOF(i) = 4_INTSIZEOF(f) = 4_INTSIZEOF("Hello,world") = 12sizeof("Hello,world") = 12
Since sizeof has the same value as _ intsizeof, why not use sizeof directly? Why is it so complicated to write? The answer is to byte alignment (no matter 32-bit or 64-bit machines, sizeof (INT) always represents the number of digits of the machine, understand it! ^_^)
Now let's look at the implementation of the variable length parameter:In fact, it is to record the address of the parameter in the stack to the AP (through a fixed parameter paramn to determine the address), and then read the value one by one.
Is there a sense of openness? At least I understand a lot and a lot.
Iii. Knowledge Extension
As you may have guessed, what do I need to expand ?! Pai_^
Two function call conventions
_ Stdcall (C ++ default)
- The parameter is pushed to the stack from right to left.
- The function is called to modify the stack.
- The function name (at the compiler level) is automatically followed by a leading underline followed by a @ symbol followed by the parameter size.
_ Cdecl (C language default)
- The parameter is pushed to the stack from right to left.
- The caller is clear about the parameters. manually clear the stack. The called function does not require the caller to pass many parameters. Too many or too few parameters are passed by the caller, and even different parameters do not generate errors in the compilation phase.
Then, the call method of the Variable Parameter Function is (or can only be) :__ cdecl.
I was planning to write a little more extensions, and I was worried that the text would not be correct. So if you are interested, you can go to the articles in the reference materials and have some detailed introductions.
References
- Http://www.cplusplus.com/reference/clibrary/cstdarg/va_start/
- Http://www.cplusplus.com/reference/clibrary/cstdarg/va_end/
- Http://www.cplusplus.com/reference/clibrary/cstdarg/va_list/
- Http://www.cplusplus.com/reference/clibrary/cstdarg/va_arg/
- Http://51hired.com/questions/13278? Sort = oldest
- Http://www.cnblogs.com/diyunpeng/archive/2010/01/09/1643160.html
- Http://blog.csdn.net/huanjieshuijing/article/details/5822942
- Http://baike.baidu.com/view/1280676.htm