C + + variable parameter function implementation

Source: Internet
Author: User

Let's briefly summarize the basic usage:

1 voidSumintN, ...)2 {3Va_list arg_ptr = NULL;//Request a pointer4 5Va_start (arg_ptr, N);//set the pointer to point to6 7Va_arg (Arg_ptr,int);//Loop takes parameters out one by one8 9Va_end (ARG_PTR);//Release WorkTen}

1. The principle of variable parameter function

C + + function parameters are stored in the stack area, and the parameters of the stack is from the right side of the parameter, that is, the last parameter into the stack, and the first argument into the stack, so, according to the Stack's LIFO nature, the function always find the first parameter. Therefore, the implementation of the variable parameter function must be able to get from the known parameters to the number of parameters required by the function;

For example, the printf function, the first parameter is a format string, and the number of parameters required later can be obtained from the format string.

2. Design of variable parameter function

Standard header file Stdarg.h provides a set of implementation mechanisms for variable parameter functions, so it is necessary to write a variable parameter function to include the header file. #include <stdarg.h>

The c medium Variable length argument header file Stdarg.h provides a data type of va-list and three macros (Va-start, Va-arg, and Va-end), which are used to test the variable parameter table when the called function does not know the number and type of arguments. This provides a convenient and efficient way to access variable parameters. Va-list is a char-type pointer that declares a variable of type va-list when the called function uses a mutable parameter, which is used to point to the location of the information required by Va-arg and Va-end.

VC + + in Va_list, Va_start, Va_arg, va_end details:

    • Va_list:
Char *  

    • Va_start:
#define _intsizeof (n)   #define _crt_va_start (AP,V)  (AP = (va_list) _addressof (v) + _ Intsizeof (v))

Function: The first parameter of Va_start is the va_list variable pointer, the second parameter should pass the last fixed parameter of the variable parameter function, its function is to make the pointer of the va_list type point to the first deformable parameter of the variable parameter function.

    • Va_arg:
#define _crt_va_arg (ap,t) (    * (t *) (AP + = _intsizeof (t))-_intsizeof (t)))

Function: Va_arg macro from the above implementation code can be seen, it has two functions: the first is to make the va_list type pointer to the next variable parameter; the second is to get the value that the Va_list type pointer points to. The first parameter of the VA_ARG macro is the va_list variable, The second parameter is the data type of the parameter to be obtained.

    • Va_end:
#define _crt_va_end (AP)      (AP = (va_list) 0)

Role: Va_end is primarily for resource release, so that pointers of type va_list do not point to any memory.

From the above analysis of Va_list, Va_start, Va_arg, va_end, to write variable-length parametric functions, we first define a va_list type of variable arg_ptr, and then use Va_start (arg_ptr,arg_ list) macro makes the variable arg_ptr point to a deformable parameter before you can use Va_arg to get the values of each parameter, and in the end you must use the Va_end macro to release the resource.

3. Realization of variable length parameter function

Analysis:

In the design analysis above, there are two key points for the use of Va_arg macros, the first is the number of parameters of the deformable parameter, and the second is the parameter type of the deformable parameter, because the va_arg is addressable according to the type of the parameter, and if you know the number of parameters and the type of the parameters, the implementation of the variable parameter function is simpler , we have to write some logic to judge the implementation.

Example 1: Implementing the SUM function of a variable parameter list

1 intSumintN, ...)2 {3Va_list arg_ptr =NULL;4     inti =0, nres =0;5 Va_start (arg_ptr, n);6      for(; i < n; + +)i)7     {8         inttemp = Va_arg (arg_ptr,int);9Nres + =temp;Ten     } One va_end (arg_ptr); A     returnnres; -}

Note: The implementation of this example is simple, the fixed parameter of the function indicates the number of function parameters, and secondly, the type of the parameter is fixed to int type.

Example 2: Implement your own printf function

1 voidmyprintf (Char*Strformat, ...)2 {3     if(NULL = =Strformat)4         return;5     Charstrinfo[ +] = {0};6Va_list arg_ptr =NULL;7 Va_start (arg_ptr, Strformat);8 vsprintf (Strinfo, Strformat, arg_ptr);9 va_end (arg_ptr);Ten fputs (Strinfo, stdout); One}

Note: Although the number of function parameters and parameters is unknown, but the use of the vsprintf function, the implementation of the code is relatively simple, as to the Vsprinf function is not known to Baidu, Google

The complete code:

1#include <iostream>2#include <stdarg.h>3  4 intSumintN, ...)5 {6Va_list arg_ptr =NULL;7     inti =0, nres =0;8 Va_start (arg_ptr, n);9      for(; i < n; + +)i)Ten     { One         inttemp = Va_arg (arg_ptr,int); ANres + =temp; -     } - va_end (arg_ptr); the     returnnres; - } -   - voidmyprintf (Char*Strformat, ...) + { -     if(NULL = =Strformat) +         return; A     Charstrinfo[ +] = {0}; atVa_list arg_ptr =NULL; - Va_start (arg_ptr, Strformat); - vsprintf (Strinfo, Strformat, arg_ptr); - va_end (arg_ptr); - fputs (Strinfo, stdout); - } in   - intMain () to { +myprintf ("%d\n", SUM (3, $, the, $, -));  -     return 0; the}
Test

C + + variable parameter function implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.