I. Starting with printf ()
Prototype: int printf (const char * format ,...);
The format parameter indicates how to format string commands ,...
Optional parameter. The parameter passed to "..." during the call is optional and depends on the actual situation.
The system provides vprintf series string formatting functions for programmers to encapsulate their own I/O functions.
Int vprintf/vscanf (const char * format, va_list AP); // formatted string from standard input/output
Int vfprintf/vfsacanf (File * stream, const char * format, va_list AP); // stream from File
Int vsprintf/vsscanf (char * s, const char * format, va_list AP); // from the string
// Example 1: format a file stream for log files.
File * logfile;
Int writelog (const char * format ,...)
{
Va_list arg_ptr;
Va_start (arg_ptr, format );
Int nwrittenbytes = vfprintf (logfile, format, arg_ptr );
Va_end (arg_ptr );
Return nwrittenbytes;
}
Ii. Va Function Definition and VA macro
C supports the VA function. c ++ supports the VA function as an extension of C. It is not recommended in C ++, the polymorphism introduced by C ++ can also implement functions with variable parameter numbers. However, C ++'s overload function can only be a limited number of foreseeable parameters. In comparison, the VA function in C can define an infinite number of overload functions equivalent to C ++. In this regard, C ++ is powerless. The advantages of VA functions are convenience and ease of use, which can simplify the code. In order to unify the implementation on different hardware architectures and hardware platforms and increase code portability, the C compiler provides a series of macros to shield the differences caused by different hardware environments.
In ansi c, VA macros are defined in stdarg. H. They include va_list, va_start (), va_arg (), and va_end ().
Iii. How does the compiler implement va?
Simply put, the implementation of the VA function is the use and control of parameter pointers.
Typedef char * va_list; // va_list definition on the x86 Platform
The fixed parameter section of the function can be obtained directly from the parameter name when the function is defined. For the optional parameter section, first point the pointer to the first optional parameter, and then move the pointer backward, determine whether all parameters have been obtained based on the comparison with the end mark. Therefore, the ending mark in the VA function must be agreed in advance. Otherwise, the pointer will point to an invalid memory address, resulting in an error.
Here, the moving Pointer Points to the next parameter, so what is the offset when the pointer is moved? There is no specific answer, because the memory alignment problem is involved here, memory alignment is closely related to the specific hardware platform used. For example, the well-known 32-bit x86 Platform requires that all variable addresses must be multiples of 4 (sizeof (INT) = 4 ). In the VA mechanism, macro _ intsizeof (n) is used to solve this problem. Without these macros, VA portability is impossible.
First, we will introduce macro _ intsizeof (n), which is used to determine the size of the memory occupied by the variable, which is the basis for implementing va.
# DEFINE _ intsizeof (N) (sizeof (n) + sizeof (INT)-1 )&~ (Sizeof (INT)-1 ))
# Define va_start (AP, V) (AP = (va_list) & V + _ intsizeof (V) // The first optional parameter address
# Define va_arg (AP, t) (* (T *) (AP + = _ intsizeof (t)-_ intsizeof (t) // The next parameter address
# Define va_end (AP) (AP = va_list0) // set the pointer to invalid
1. va_arg has two roles: return the current parameter and point the parameter pointer to the next parameter.
The definition of the va_arg macro is awkward. If we split it into two statements, we can clearly see the two responsibilities it has accomplished.
# Define va_arg (AP, t) (* (T *) (AP + = _ intsizeof (t)-_ intsizeof (t) // The next parameter address
// Split (* (T *) (AP + = _ intsizeof (t)-_ intsizeof (t):
/* Pointer AP points to the address of the next parameter */
1). AP + = _ intsizeof (t); // currently, the AP has pointed to the next Parameter
/* AP obtains the address of the current parameter after subtracting the size of the current parameter, and then returns its value after forced type conversion */
2). Return * (T *) (ap-_ intsizeof (t ))
Looking back to formatting commands such as % d % s of the printf/scanf series functions, we can easily understand their usage-explicitly specify the type of mandatory conversion of parameters.
(Note: printf/scanf is not implemented using va_xxx, but the principle is consistent .)
2. va_end is simple, just to void the pointer.
# Define va_end (AP) (AP = (va_list) 0) // x86 Platform