1013-----C Language----------The use of several Va_ macro functions

Source: Internet
Author: User

Recently in the Apue with the source code, see its error handling file used in the variable parameter list (below), just recently always see these functions in front of the eyes, so just make a break. Ha ha.

#include "apue.h" #include <errno.h>/* for definition of errno */#include <stdarg.h>/* ISO C variable aruments */static voiderr_doit (int, int, const char *, va_list),/* * Nonfatal error related to a system call. * Print a message and return. */voiderr_ret (const char *FMT, ...) {Va_listap;va_start (AP, FMT); Err_doit (1, errno, FMT, AP); Va_end (AP);} /* * Fatal Error related to a system call. * Print a message and terminate. */voiderr_sys (const char *FMT, ...) {Va_listap;va_start (AP, FMT); Err_doit (1, errno, FMT, AP); Va_end (AP); exit (1);} /* * Fatal error unrelated to a system call. * Error code passed as explict parameter. * Print a message and terminate. */voiderr_exit (int error, const char *FMT, ...) {Va_listap;va_start (AP, FMT); Err_doit (1, error, FMT, AP); Va_end (AP); exit (1);} /* * Fatal Error related to a system call. * Print a message, dump core, and terminate. */voiderr_dump (const char *FMT, ...) {Va_listap;va_start (AP, FMT); Err_doit (1, errno, FMT, AP); Va_end (AP); abort ();/* Dump CORE and Terminate */exit (1);/* shouldn ' t get here */}/* * nonfatal error unrelated to a system call. * Print a message and return. */voiderr_msg (const char *FMT, ...) {Va_listap;va_start (AP, FMT); Err_doit (0, 0, FMT, AP); Va_end (AP);} /* * Fatal error unrelated to a system call. * Print a message and terminate. */voiderr_quit (const char *FMT, ...) {Va_listap;va_start (AP, FMT); Err_doit (0, 0, FMT, AP); Va_end (AP); exit (1);} /* Print a message and return to caller. * Caller Specifies "Errnoflag". */static voiderr_doit (int errnoflag, int error, const char *FMT, va_list ap) {charbuf[maxline];vsnprintf (buf, MAXLINE, FMT , AP), if (Errnoflag) snprintf (Buf+strlen (BUF), Maxline-strlen (BUF), ":%s", strerror (Error)), strcat (buf, "\ n"); Fflush ( STDOUT);/* In case stdout and stderr is the same */fputs (buf, stderr); Fflush (NULL);/* Flushes all stdio output streams */ }

1. First, revisit the commonly used functions with variable parameters to format the output function family as an example:

#include <stdio.h>int printf (const char *format, ...);      //formatted to standard output int fprintf (FILE *stream, const char *format, ...);            //format to file int sprintf (char *str, const char *format, ...); formatted to string int snprintf (char *str, size_t size, const char *format, ...); /Specifies the maximum length of the formatted string # include <stdarg.h>int vprintf (const char *format, va_list AP);                 int vfprintf (FILE *stream, const char *format, va_list AP); int vsprintf (char *str, const char *format, va_list AP); int VSN printf (char *str, size_t size, const char *format, va_list AP);

The function of these two sets of functions is the same, the difference is that the following uses a variable of type va_list, that is, the va_list type is substituted for the indefinite number of arguments. The first kind of use is no longer ripe, mainly to see the second kind of usage.

2. Usage of several VA_ class macro functions
2.1 Function definitions

#include <stdarg.h>            void Va_start (Va_list ap, last);       Type Va_arg (va_list ap, type);       void Va_end (Va_list ap);       void Va_copy (va_list dest, va_list src);

Va_list//variable argument list pointer type, defined in x86 as char *;

Va_start (Va_list AP, last)//This function is used to initialize the AP so that the AP points to the first parameter of the mutable parameter list; The last variable is the one that precedes the variable argument list;

Va_arg (va_list AP, type)//returns the contents of the parameter to which the AP points, which is type,ap to the next parameter in the mutable parameter list;

Va_end (Va_list AP)//emptying Arg;

Va_copy (va_list dest, va_list src)//copy parameter list pointer.

2.2 Usage examples, it is important to note that using this function requires that you indicate the number of arguments or the contract argument list Terminator in the argument list, or there will be an out-of-bounds error.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h>int test (char * FMT, ...); int main (int argc, const char *argv[]) {    test ("test", "Hello", "World", "");    return 0;} int Test (char *fmt, ...) {    va_list ap;    char *temp;    Va_start (AP, FMT);    while (1) {        temp = Va_arg (AP, char *);        if (temp! = "") {            printf ("%s", temp);        }        else break            ;    }    Va_end (AP);    return 0;}

3. Implementation of macro functions
Through the use of the function can be seen that the key to the implementation of these macro functions is the parameter list pointer movement, different types of moving different bytes, C defines the macro is used to determine the length of the variable and the macro function to achieve the memory alignment.
#define _INTSIZEOF (N) ((sizeof (n) +sizeof (int)-1) &~ (sizeof (int)-1))

The macro functions are implemented as follows:

#define VA_START (AP, v) (AP = (va_list) &v + _intsizeof (v)) #define VA_ARG (Ap,t) (* (t *) (AP + = _intsizeof (t))-_ints Izeof (t)) #define VA_END (AP) (AP = (va_list) 0)

The second one is harder to understand, and it needs decomposition.

(1) AP + = _intsizeof (t)//ap refers to the address of a parameter down

(2) The macro function returns the current parameter return * (T *) (AP-_intsizeof (t)).

4. The case of stack frames when invoking a function
This involves the use of registers and part of the Assembly, I will look at the assembly language for a few days to summarize.

Http://blog.chinaunix.net/uid-20718384-id-3418279.html

5. Reference
Http://www.360doc.com/content/10/1220/11/1317564_79712393.shtml
Http://www.360doc.com/content/10/1220/11/1317564_79711248.shtml
Http://blog.chinaunix.net/uid-23089249-id-34493.html

1013-----C Language----------The use of several Va_ macro functions

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.