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()函数,可是当时"道行"不深或许不够细心留意,又或者我们理所当然地认为库函数规定这样就是这样,没有发现这个函数与普通的函数 ...
随机推荐
- Python进阶之返回函数
返回函数 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB"," ...
- 将数据从服务器端同步到手机上, 并且需要离线工作,Couchebase Mobile 也许是目前最好的解决方案:
将数据从服务器端同步到手机上, 并且需要离线工作,Couchebase Mobile 也许是目前最好的解决方案: 原文地址: https://www.infinum.co/the-capsized-e ...
- 在git彻底删除commit记录的方法是什么?
在github上,如果非默认分支的话,直接用以下方法: git reset --hard <commit_id> git push origin HEAD --force 如是默认分支,需 ...
- C++设计模式之工厂模式
//产品父类声明#pragma once #include "iostream" using namespace std; class Product { public: virt ...
- Oracle EBS-SQL (SYS-17):查询一张报表在哪个职责下面.sql
1.查询该报表的 concurrent_program_id SQL如下: SELECT t.application_id, t.concurrent_program_id, t.* FROM FND ...
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- 在MyEclipse中统计项目行数
今天闲来无事就把自己曾经做过的一些小项目拿出来看一下,把一些自己觉得不好的地方又又一次改一下,突然想起有人说过大学生在毕业时至少要完毕多少代码才算合格,所以我就想统计一下自己做过的项目的代码量,在网上 ...
- HDU 2254 奥运(数论+矩阵)
题目中文的不解释啊. .. 须要注意的就是:离散数学中,有向图的邻接矩阵A表示全部点之间路径长度为1的路径数量,A^n则表示路径长度为n的路径数量.故须要求某两点在(A^t1)~(A^t2)的路径数量 ...
- Objective-C 类属性和方法的訪问权限
OC中提供了4种訪问权限.@private, @public, @protected这三种和其它的C++, Java是一样的,@package这个訪问权限并非Java里的包訪问权限,OC中没有包的概念 ...
- 初识Maven
今天开始学习怎样使用maven,听起来挺神奇的东西,我们来一步一步的加以剖析. Maven的一些具体的论文的东西,网上很多博客介绍,这里我就不逐一介绍,下面我们从安装maven开始讲解: (1)Mav ...