"Go" C + + variable parameter list handles the use of macros Va_list, Va_start, Va_end

Source: Internet
Author: User

Va_list is a group of macros that solve the argument problem in the C language he has so many members:

1) va_list type variable:

#ifdef _m_alpha

typedef struct{

char* A0; /*pointertofirsthomedintegerargument*/

int offset; /*byteoffsetofnextparameter*/

}va_list;

#else

typedef char* Va_list; #endif


2) _intsizeof macro, gets the space length occupied by the type, the minimum occupancy length is an integer multiple of int: #define _INTSIZEOF (N) (sizeof (n) +sizeof (int)-1) &~ (sizeof (int)-1))

|------------------------------------------------| High Address
|-------------function return address-----------------------|

|------------.........................------------------|
|------------------------------------------------|<--va_arg after AP Point

| Nth parameter (first variable parameter) |
|------------------------------------------------|<--va_start after AP Point

| N-1 parameter (last fixed parameter) |

|------------------------------------------------|<--&v Low Address


3) 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, i.e. the last fixed parameter):
#define VA_START (Ap,v) (ap= (va_list) &v+_intsizeof (v))


4) Va_arg macro, gets the current parameter of the variable 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)))


5) Va_end macro, empty the va_list variable parameter list:

#define VA_END (AP) (ap= (va_list) 0)


Usage of va_list:
(1) First define a variable of type va_list in the function, which is a pointer to the parameter;

(2) Then use the Va_start macro to initialize the va_list variable just defined by the variable to point to the first
The address of the variable parameter;

(3) Then return the variable parameter with Va_arg, the second parameter of Va_arg is the parameter you want to return
(If the function has more than one variable parameter, call Va_arg to get each parameter in turn);

(4) Finally, the Va_end macro is used to end the variable parameter acquisition. Issues to be aware of when using va_list:
(1, the type and number of variable parameters are completely controlled by the program code, it can not intelligently 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 to the variable parameter of the function of the prototype check is not strict, bad for programming error.) not good for us to write high-quality code;
Summary: The function principle of the variable parameter is actually very simple, and the VA series is defined by the macro definition, and the implementation is related to the stack. When we write a variable-parameter C function, there are pros and cons, so on unnecessary occasions, we do not need to use variable parameters, if in C + +, we should use C + + polymorphism to achieve the function of variable parameters, as far as possible to avoid the C language approach.

Sample program:

#include <iostream>#include<stdarg.h>using namespacestd;intSumChar*msg, ...);intmy_vsprintf (Char*buf,Char*format, ...);intMain () {sum ("The sum of the list is:",1,2,3,4,5,6,7,8,9,Ten,0); cout<<Endl; Charbuf[ the]; my_vsprintf (BUF,"%my name is%s and I am%d years.","Ben", -); cout<< buf <<Endl; System ("Pause"); return 0;}intSumChar*msg, ...)    {Va_list St;    Va_start (St, MSG); intTotal =0; inttmp;  while(tmp = Va_arg (ST,int)) !=0) { Total+=tmp;    } va_end (ST); cout<<"The sum of the list is:"<<Total ; return 0;}intmy_vsprintf (Char*buf,Char*format, ...)    {Va_list St;    Va_start (ST, format);    vsprintf (buf, format, ST); /***************************************************************************/    /*function Name: vsprintf/* Function: Send formatted output to the string  /* Return value: Returns the length of the generated string normally (minus \), the error condition returns a negative number/* Usage: int vsprintf (char *string,       Char *format, va_list param);                                                                            /* Writes a string param formatted as format format, */* Note: The function will have a memory overflow situation, we recommend the use of vsnprintf */    /***************************************************************************/Va_end (ST); return 0;}

A template with another dump data attached

#ifdef _time_debug_ #define Offline_log (FMT, args ...)  Do {    while (0)#else#define offline_log (FMT, args ...) #endif

"Go" C + + variable parameter list handles the use of macros Va_list, Va_start, Va_end

Related Article

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.