Principle of variable parameter functions

Source: Internet
Author: User

Variable parameters.
1: there must be an advance parameter (I .e :... must have a parameter) to calculate the address of the first unknown parameter. after knowing the address of the first unknown parameter, You Can format the string based on FMT and calculate the remaining parameter addresses in sequence.
Sprintf () prototype: sprintf (char * buffer,Const char * FMT,...), Where FMT is an advance Parameter
2: For each variable elaboration function, both the creator and the user must have a parameter usage convention. Otherwise, it will be messy.

3: technical basis for Variable Function implementation
1: The size of all parameters at the Assembly level is an integer multiple of 4 bytes. The Char and short types are also extended to 4 bytes.
The technology of extending any length type to an integer multiple of 4 bytes is
_ Intsizeof (N) (sizeof (n) + sizeof (INT)-1 )&~ (Sizeof (INT)-1 ))
(Sizeof (n) + 4-1 )&~ (4-1 ))
_ Intsizeof (char *) = 4
_ Intsizeof (2 byte) = 4
_ Intsizeof (double) = 8

2: with the above knowledge, the following is to remove parameters from variable parameters one by one.
# Define va_start (AP, V) (AP = (va_list) & V + _ intsizeof (v ))
//: Get the address of known parameters,
// AP is the first unknown parameter address, and V is the aforementioned advance parameter FMT.
// Eg: Fun (int n,...) V is the address of N.

# Define va_arg (AP, T )\
(* (T *) (AP + = _ intsizeof (t)-_ intsizeof (t )))

After va_arg () is replaced, the AP points to the next type address of the T type. Va_arg itself is replaced with the value of the parameter corresponding to the T type. The T type is generally converted from the FMT formatting string according to % d, % C, etc.
For example, 1: int A = va_art (AP, INT );

For example, 2: Fun (int n ,...)
{
...
Char * pchar = va_arg (AP, char *)
Int TMP = va_arg (AP, INT)
Double dd = va_arg (AP, double)
}
So,... it must be char *, Int, and double Parameter order !! If not, the program will occasionally crash !! This is

The reason is that C is not type-safe. Boost: format seems to provide type-safe format conversion, which can replace sprintf.
For sprintf (), the va_arg (AV, v) V value is extracted from char * FMT.
 
Appendix:

3) // sample code 2: extension-implement simple variable parameter functions by yourself. (Macros such as va_list are not used)
The following is a simple implementation of the printf function. For more information, see <The C programming language>.
# Include "stdio. H"
# Include "stdlib. H"
Void myprintf (char * FMT,...) // a simple implementation similar to printf. // The parameters must be of the int type.
{
Char * parg = NULL; // equivalent to the original va_list
Char C;

Parg = (char *) & FMT; // do not write P = FMT !! Because the address must be set for the // parameter, instead of the value.
Parg + = sizeof (FMT); // equivalent to the original va_start
 
Do
{
C = * FMT;
If (C! = '% ')
{
Putchar (c); // output character as is
}
Else
{
// Output data by formatted characters
Switch (* ++ FMT)
{
Case 'D ':
Printf ("% d", * (int *) parg ));
Break;
Case 'X ':
Printf ("% # X", * (int *) parg ));
Break;
Default:
Break;
}
Parg + = sizeof (INT); // equivalent to the original va_arg
}
++ FMT;
} While (* FMT! = '/0 ');
Parg = NULL; // equivalent to va_end
Return;
}
Int main (INT argc, char * argv [])
{
Int I = 1234;
Int J = 5678;

Myprintf ("the first test: I = % d/N", I, j );
Myprintf ("The secend test: I = % d; % x; j = % d;/N", I, 0 xabcd, J );
System ("pause ");
Return 0;
}

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.