在linux内核中经常看到可变参数函数,如我们熟悉的printf、printk、scanf、sscanf.....这些函数都使用了va_start,va_arg,va_end。
我们首先得知道调用一个含参数的函数时,输入参数少的利用通用寄存器存储,输入参数多的一部分存入通用寄存器,一部分存入堆栈。在VC6.0中,在定义函数时可以在前面加入_cdecl(其实默认就是),使得输入参数全部存入堆栈,这样每个参数在内存中都是相邻的。
在看va_start,va_arg,va_end的作用,在VC6.0这3个宏定义在stdarg.h头文件中:
/*** *stdarg.h - defines ANSI-style macros for variable argument functions * * Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved. * *Purpose: * This file defines ANSI-style macros for accessing arguments * of functions which take a variable number of arguments. * [ANSI] * * [Public] * ****/ #if _MSC_VER > 1000 #pragma once #endif #ifndef _INC_STDARG #define _INC_STDARG #if !defined(_WIN32) && !defined(_MAC) #error ERROR: Only Mac or Win32 targets supported! #endif #ifdef _MSC_VER /* * Currently, all MS C compilers for Win32 platforms default to 8 byte * alignment. */ #pragma pack(push,8) #endif /* _MSC_VER */ #ifdef __cplusplus extern "C" { #endif #ifndef _VA_LIST_DEFINED #ifdef _M_ALPHA typedef struct { char *a0; /* pointer to first homed integer argument */ int offset; /* byte offset of next parameter */ } va_list; #else typedef char * va_list; #endif #define _VA_LIST_DEFINED #endif #ifdef _M_IX86 #define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) ) #define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) ) #define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) ) #define va_end(ap) ( ap = (va_list)0 ) #elif defined(_M_MRX000) /* Use these types and definitions if generating code for MIPS */ #define va_start(ap,v) ap = (va_list)&v + sizeof(v) #define va_end(list) #define va_arg(list, mode) ((mode *)(list = (char *) ((((int)list + (__builtin_alignof(mode)<=4?3:7)) & (__builtin_alignof(mode)<=4?-4:-8))+sizeof(mode))))[-1] /* +++++++++++++++++++++++++++++++++++++++++++ Because of parameter passing conventions in C: use mode=int for char, and short types use mode=double for float types use a pointer for array types +++++++++++++++++++++++++++++++++++++++++++ */ #elif defined(_M_ALPHA) /* Use these types and definitions if generating code for ALPHA */ /* * The Alpha compiler supports two builtin functions that are used to * implement stdarg/varargs. The __builtin_va_start function is used * by va_start to initialize the data structure that locates the next * argument. The __builtin_isfloat function is used by va_arg to pick * which part of the home area a given register argument is stored in. * The home area is where up to six integer and/or six floating point * register arguments are stored down (so they can also be referenced * by a pointer like any arguments passed on the stack). */ extern void * __builtin_va_start(va_list, ...); #ifdef _CFRONT #define __builtin_isfloat(a) __builtin_alignof(a) #endif #define va_start(list, v) __builtin_va_start(list, v, 1) #define va_end(list) #define va_arg(list, mode) ( *( ((list).offset += ((int)sizeof(mode) + 7) & -8) , (mode *)((list).a0 + (list).offset - ((__builtin_isfloat(mode) && (list).offset <= (6 * 8)) ? (6 * 8) + 8 : ((int)sizeof(mode) + 7) & -8) ) ) ) #elif defined(_M_PPC) /* Microsoft C8 front end (used in Motorola Merged compiler) */ /* bytes that a type occupies in the argument list */ #define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) ) /* return 'ap' adjusted for type 't' in arglist */ #define _ALIGNIT(ap,t) ((((int)(ap))+(sizeof(t)<8?3:7)) & (sizeof(t)<8?~3:~7)) #define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) ) #define va_arg(ap,t) ( *(t *)((ap = (char *) (_ALIGNIT(ap, t) + _INTSIZEOF(t))) - _INTSIZEOF(t)) ) #define va_end(ap) ( ap = (va_list)0 ) #elif defined(_M_M68K) #define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) ) #define va_start(ap,v) ( ap = (va_list)&v + (sizeof(v) < sizeof(int) ? sizeof(v) : _INTSIZEOF(v)) ) #define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) ) #define va_end(ap) ( ap = (va_list)0 ) #elif defined(_M_MPPC) #define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) ) #define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) ) #define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) ) #define va_end(ap) ( ap = (va_list)0 ) #else /* A guess at the proper definitions for other platforms */ #define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) ) #define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) ) #define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) ) #define va_end(ap) ( ap = (va_list)0 ) #endif #ifdef __cplusplus } #endif #ifdef _MSC_VER #pragma pack(pop) #endif /* _MSC_VER */ #endif /* _INC_STDARG */
里面有不同架构的定义,其实都是一个意思:
va_start(ap,v) : 将v变量下一个变量的地址赋给ap;
m = va_arg(ap,t) : t一般是变量类型,就是在ap地址处获取类型为t的变量值赋值给m, 然后ap = ap + sizeof(t)(忽略对齐),也就是指向下一个变量地址。
va_end(ap) : 参数提取完毕,ap清空为0,防止再让ap指向非法内存。
大概就是这样的意思吧,下面我把我的测试代码放上来:
#include <stdio.h> #include <stdarg.h> int _cdecl square_sum(int count, ... ) { va_list args; int sum = 0, tmp; va_start(args, count); while(count) { tmp = va_arg(args, int); sum += (tmp * tmp); count--; } va_end(args); return sum; } void main(void) { printf("sum = %d ", square_sum(1,1)); printf("sum = %d ", square_sum(2,1,2)); printf("sum = %d ", square_sum(3,1,2,3)); printf("sum = %d ", square_sum(4,1,2,3,4)); }
这个代码是求多个数的平方和。
结果: