可变参数函数实现
va_list,va_start,va_arg,va_end
va可变参数意思,variable-argument。
1. 头文件及实现
linux中定义在gcc头文件中,stdarg.h中。
41 #ifndef __GNUC_VA_LIST
42 #define __GNUC_VA_LIST
43 typedef __builtin_va_list __gnuc_va_list;
44 #endif
45
46 /* Define the standard macros for the user,
47 if this invocation was from the user program. */
48 #ifdef _STDARG_H
49
50 #define va_start(v,l) __builtin_va_start(v,l)
51 #define va_end(v) __builtin_va_end(v)
52 #define va_arg(v,l) __builtin_va_arg(v,l)
加builtin前缀的都是编译器内置函数,在机器上找不到源代码。
GCC provides a large number of built-in functions other than the ones mentioned above. Some of these are for internal use in the processing of exceptions or variable-length argument lists and are not documented here because they may change from time to time; we do not recommend general use of these functions.
The remaining functions are provided for optimization purposes.
GCC includes built-in versions of many of the functions in the standard C library. These functions come in two forms: one whose names start with the __builtin_
prefix, and the other without. Both forms have the same type (including prototype), the same address (when their address is taken), and the same meaning as the C library functions even if you specify the -fno-builtin option see C Dialect Options). Many of these functions are only optimized in certain cases; if they are not optimized in a particular case, a call to the library function is emitted.
在VS2008上的一种实现:
1: ///stdarg.h
2: #define va_start _crt_va_start
3: #define va_arg _crt_va_arg
4: #define va_end _crt_va_end
5:
6: ///vadefs.h
7: #define _ADDRESSOF(v) ( &reinterpret_cast<const char &>(v) )
8: typedef char * va_list;
9: #define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
10: #define _crt_va_start(ap,v) ( ap = (va_list)_ADDRESSOF(v) + _INTSIZEOF(v) )
11: #define _crt_va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
12: #define _crt_va_end(ap) ( ap = (va_list)0 )
其实际就是利用函数参数入栈的顺序来计算参数地址,从而获知参数值。
函数参数是以数据结构:栈的形式存取,从右至左入栈。
首先是参数的内存存放格式:参数存放在内存的堆栈段中,在执行函数的时候,从最后一个开始入栈。因此栈底高地址,栈顶低地址,举个例子如下:
void func(int x, float y, char z);
那么,调用函数的时候,实参 char z 先进栈,然后是 float y,最后是 int x,因此在内存中变量的存放次序是 x->y->z,因此,从理论上说,我们只要探测到任意一个变量的地址,并且知道其他变量的类型,通过指针移位运算,则总可以顺藤摸瓜找到其他的输入变量。
2. 应用
一种实现可查到的实现:
typedef char* va_list;
void va_start ( va_list ap, prev_param ); /* ANSI version */
type va_arg ( va_list ap, type );
void va_end ( va_list ap );
va_list 是一个字符指针,可以理解为指向当前参数的一个指针,取参必须通过这个指针进行。
3. 使用总结
1)标准C库的中的三个宏的作用只是用来确定可变参数列表中每个参数的内存地址,编译器是不知道参数的实际数目的。
2)在实际应用的代码中,程序员必须自己考虑确定参数数目的办法,如
⑴在固定参数中设标志-- printf函数就是用这个办法。
⑵在预先设定一个特殊的结束标记,就是说多输入一个可变参数,调用时要将最后一个可变参数的值设置成这个特殊的值,在函数体中根据这个值判断是否达到参数的结尾。本文前面的代码就是采用这个办法.
无论采用哪种办法,程序员都应该在文档中告诉调用者自己的约定。
3)实现可变参数的要点就是想办法取得每个参数的地址,取得地址的办法由以下几个因素决定:
①函数栈的生长方向
②参数的入栈顺序
③CPU的对齐方式
④内存地址的表达方式
结合源代码,我们可以看出va_list的实现是由④决定的,_INTSIZEOF(n)的引入则是由③决定的,他和①②又一起决定了va_start的实现,最后va_end的存在则是良好编程风格的体现,将不再使用的指针设为NULL,这样可以防止以后的误操作。
4)取得地址后,再结合参数的类型,程序员就可以正确的处理参数了。
可变参数的函数原理其实很简单,而va系列是以宏定义来定义的,实现跟堆栈相关.我们写一个可变函数的C函数时,有利也有弊,所以在不必要的场合,我们无需用到可变参数.如果在C++里,我们应该利用C++的多态性来实现可变参数的功能,尽量避免用C语言的方式来实现.
4. 一个示例
对printf一层封装:
void app_printf(int log_level, const char *format, …)
{
va_list ap;
if(log_level <= current_log_level)
{
if(log_level == KDUMMY)
return;
va_start(ap, format);
vprintf(format, ap);
va_end(ap);
}
}
注:vprintf以va_list为参数打印输出到标准输出。
The functions printf() and vprintf() write output to stdout, the standard output stream。
int vprintf(const char *format, va_list ap);
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
void foo(char *fmt, ...)
{
va_list ap;
int d;
char c, *s;
va_start(ap, fmt);
while(*fmt){
switch(*fmt++){
case 's':
s = va_arg(ap, char *);
printf("string %s\n", s);
break;
case 'd':
d = va_arg(ap, int);
printf("int %d\n", d);
break;
case 'c':
/* need a cast here since va_arg only
takes fully promoted types */
c = (char) va_arg(ap, int);
printf("char %c\n", c);
break;
default :
break;
}
}
va_end(ap);
}
int main(int argc, char **argv)
{
foo("dscs", 100, "mouse", 'A', "end");
printf("--------------------\n");
printf("%d%s%c%s\n", 100, "mouse", 'A', "end");
return 0;
}
执行结果:
~$gcc valist.c -Wall
~$./a.out
int 100
string mouse
char A
string end
--------------------
100mouseAend
参考:
原文链接: https://www.cnblogs.com/embedded-linux/p/5575437.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/235021
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!