C++可变参数的另一种实现
大家熟知的C库函数printf函数就是一个可变参数函数,它是怎么实现的呢?不过他实现是有条件的,必须函数参数的入栈顺序为从右向左的顺序,也即函数的形参,在函数调用之前,必须是最右边的参数先入栈,并且参数都必须通过栈传递,以1个例子说明,如函数func(arg1, arg2,arg3),那么函数的堆栈应是:

ebp是帧指针寄存器,一般用来存取堆栈,有了堆栈结构,下面我们看看C可变参数的具体实现原理:
- #include <stdio.h>
- enum {
- ptChar,
- ptInt,
- ptFloat,
- ptDouble,
- };
- void printSum(unsigned long paramFormat, ...)
- {
- /*高16位为可变参数类型,低16位为可变参数个数*/
- int paramType = (paramFormat >> 16);
- int paramNum = paramFormat & 0xffff;
- /*¶mFormat = ebp + 8,第一个参数的地址*/
- unsigned long *pArg = ¶mFormat;
- /*ebp + 0x0c, 第二个参数地址*/
- pArg++;
- switch(paramType)
- {
- case ptChar:
- {
- int sum = 0;
- for (int i = 0; i < paramNum; i++)
- {
- char *pValue = (char *)pArg;
- sum += *pValue;
- pArg++;
- }
- printf("%d\n", sum);
- }
- break;
- case ptInt:
- {
- int sum = 0;
- for (int i = 0; i < paramNum; i++)
- {
- int *pValue = (int *)pArg;
- sum += *pValue;
- pArg++;
- }
- printf("%d\n", sum);
- }
- break;
- case ptFloat:
- {
- float sum = 0;
- /**/
- pArg++;
- /*浮点参数,堆栈占8个字节,所以指针偏移为8*/
- for (int i = 0; i < paramNum; i++)
- {
- float *pValue = (float *)pArg;
- sum += *pValue;
- pArg++;
- pArg++;
- }
- printf("%f\n", sum);
- }
- break;
- case ptDouble:
- {
- double sum = 0;
- /*双精度浮点参数,堆栈占8个字节,所以指针偏移为8*/
- for (int i = 0; i < paramNum; i++)
- {
- double *pValue = (double *)pArg;
- sum += *pValue;
- pArg++;
- pArg++;
- }
- printf("%f\n", sum);
- }
- break;
- default:
- printf("unknowned type!\n");
- break;
- }
- }
- void main()
- {
- unsigned long paramFormat = 3;
- char a = 1, b = 2, c = 3;
- printSum(paramFormat, a, b, c);
- paramFormat = ptInt << 16;
- paramFormat += 3;
- int ia = 1, ib = 2, ic = 3;
- printSum(paramFormat, ia, ib, ic);
- paramFormat = ptFloat << 16;
- paramFormat += 3;
- float fa = 1, fb = 2, fc = 3;
- printSum(paramFormat, fa, fb, fc);
- paramFormat = ptDouble << 16;
- paramFormat += 3;
- double da = 1, db = 2, dc = 3;
- printSum(paramFormat, da, db, dc);
- }
上面这种方法对函数参数的入栈顺序有限制,必须从右向左入栈,这就是为什么pascal调用方式不能实现printf的原因,并且函数形参都要通过栈来传递,这对有些编译器为了优化处理,函数参数通过寄存器来传递,从而不满足要求。鉴于次,本文采用C++的默认形参实现可变参数的方法,没有上面的这些限制,下面是实现代码:
- #include <stdio.h>
- enum {
- ptChar,
- ptInt,
- ptFloat,
- ptDouble,
- };
- void printSum(unsigned long paramType,
- void *arg1 = NULL,
- void *arg2 = NULL,
- void *arg3 = NULL,
- void *arg4 = NULL,
- void *arg5 = NULL,
- void *arg6 = NULL,
- void *arg7 = NULL,
- void *arg8 = NULL,
- void *arg9 = NULL,
- void *arg10 = NULL)
- {
- void *arg[10] = {
- arg1,
- arg2,
- arg3,
- arg4,
- arg5,
- arg6,
- arg7,
- arg8,
- arg9,
- arg10,
- };
- switch(paramType)
- {
- case ptChar:
- {
- int sum = 0;
- for (int i = 0; i < 10; i++)
- {
- if (arg[i] != NULL)
- {
- char *pValue = (char *)arg[i];
- sum += *pValue;
- }
- else
- break;
- }
- printf("%d\n", sum);
- }
- break;
- case ptInt:
- {
- int sum = 0;
- for (int i = 0; i < 10; i++)
- {
- if (arg[i] != NULL)
- {
- int *pValue = (int *)arg[i];
- sum += *pValue;
- }
- else
- break;
- }
- printf("%d\n", sum);
- }
- break;
- case ptFloat:
- {
- float sum = 0;
- for (int i = 0; i < 10; i++)
- {
- if (arg[i] != NULL)
- {
- float *pValue = (float *)arg[i];
- sum += *pValue;
- }
- else
- break;
- }
- printf("%f\n", sum);
- }
- break;
- case ptDouble:
- {
- double sum = 0;
- for (int i = 0; i < 10; i++)
- {
- if (arg[i] != NULL)
- {
- double *pValue = (double *)arg[i];
- sum += *pValue;
- }
- else
- break;
- }
- printf("%f\n", sum);
- }
- break;
- default:
- printf("unknowned type!\n");
- break;
- }
- }
- void main()
- {
- unsigned long paramType = ptChar;
- char a = 1, b = 2, c = 3;
- printSum(paramType, &a, &b, &c);
- paramType = ptInt;
- int ia = 1, ib = 2, ic = 3;
- printSum(paramType, &ia, &ib, &ic);
- paramType = ptFloat;
- float fa = 1, fb = 2, fc = 3;
- printSum(paramType, &fa, &fb, &fc);
- paramType = ptDouble;
- double da = 1, db = 2, dc = 3;
- printSum(paramType, &da, &db, &dc);
- }
http://blog.csdn.net/rabinsong/article/details/8946514
C++可变参数的另一种实现的更多相关文章
- c 可变参数 定义可变参数的函数
定义可变参数的函数,需要在stdarg.h头文件中定义的va_list类型和va_start.va_arg.va_end三个宏. 定义可变参数函数 va_list ap; //实际是定义一个指针va ...
- 理解 Python 中的可变参数 *args 和 **kwargs:
默认参数: Python是支持可变参数的,最简单的方法莫过于使用默认参数,例如: def getSum(x,y=5): print "x:", x print "y:& ...
- Java 传递可变参数和方法重载
形式:类型... 参数名 示例:public void show(int... a) {}; 可变参数在方法中被当作数组来处理 可变参数传值的四种方式: 一个值也不传,可变参数会接收到长度为0的数组 ...
- define可变参数,float数据传输
define可变参数 一般在调试打印Debug信息的时候, 需要可变参数的宏. 从C99开始可以使编译器标准支持可变参数宏(variadic macros), 另外GCC也支持可变参数宏, 但是两种在 ...
- [11 Go语言基础-可变参数函数]
[11 Go语言基础-可变参数函数] 可变参数函数 什么是可变参数函数 可变参数函数是一种参数个数可变的函数. 语法 如果函数最后一个参数被记作 ...T ,这时函数可以接受任意个 T 类型参数作为最 ...
- 嵌入式C语言自我修养 12:有一种宏,叫可变参数宏
12.1 什么是可变参数宏 在上面的教程中,我们学会了变参函数的定义和使用,基本套路就是使用 va_list.va_start.va_end 等宏,去解析那些可变参数列表我们找到这些参数的存储地址后, ...
- 大数据学习day13------第三阶段----scala01-----函数式编程。scala以及IDEA的安装,变量的定义,条件表达式,for循环(守卫模式,推导式,可变参数以及三种遍历方式),方法定义,数组以及集合(可变和非可变),数组中常用的方法
具体见第三阶段scala-day01中的文档(scala编程基础---基础语法) 1. 函数式编程(https://www.cnblogs.com/wchukai/p/5651185.html): ...
- C可变参数的函数
我们实现一个简单的printf函数(可变参数) #include <stdio.h> #include <stdarg.h> void myprintf(const char ...
- 可变参数列表与printf()函数的实现
问题 当我们刚开始学习C语言的时候,就接触到printf()函数,可是当时"道行"不深或许不够细心留意,又或者我们理所当然地认为库函数规定这样就是这样,没有发现这个函数与普通的函数 ...
随机推荐
- DevExpress中GridControl的属性设置
1.隐藏最上面的GroupPanel gridView1.OptionsView.ShowGroupPanel=false; 2.得到当前选定记录某字段的值 sValue=Table.Rows[gri ...
- C语言学习——C程序的运行机理
预处理: #include<xxx> 尖括号表示库文件:#include"xxx" 双引号表示自己写的文件. #include后面的文件格式允许多种,但若要是" ...
- Oracle查看被锁的表和解锁[转]
查看被锁的表 select p.spid,a.serial#, c.object_name,b.session_id,b.oracle_username,b.os_user_name from v$p ...
- css3 drop-shaow阴影效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- Ajax中send方法的使用
Ajax中send方法参数的使用 一般情况下,使用Ajax提交的参数多是些简单的字符串,可以直接使用GET方法将要提交的参数写到open方法的url参数中,此时send方法的参数为null. 例如 : ...
- 走进C标准库(1)——assert.h,ctype.h
默默觉得原来的阅读笔记的名字太土了,改了个名字,叫做走进C标准库. 自己就是菜鸟一只,第一次具体看C标准库,文章参杂了对<the standard C library>的阅读和对源码的一些 ...
- 复习-C语言内嵌汇编-初级(2)
汇编取出内存中的值 # include <stdio.h> int main() { ; ; int *p = &i; //ret = *p; __asm__( "ldr ...
- Oracle EBS-SQL (SYS-17):查询一张报表在哪个职责下面.sql
1.查询该报表的 concurrent_program_id SQL如下: SELECT t.application_id, t.concurrent_program_id, t.* FROM FND ...
- Debug程序无法运行解决
说明:文章内容部分参考网络上的解决思路. 在没有安装Microsoft Visual Studio的系统上,Debug版本无法正常运行.这是由于缺少vs运行时库引起的. 以vs2005为例.开发机用v ...
- 浅谈Web Api配合SignalR的跨域支持
最近接手的一个项目中,涉及到一个简单的消息模块,由于之前有简单了解过SignalR,所以打算尝试着摸索摸索~! 首先,通过Nuget管理器添加Microsoft ASP.NET SignalR引用~目 ...