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

ebp是帧指针寄存器,一般用来存取堆栈,有了堆栈结构,下面我们看看C可变参数的具体实现原理:

  1. #include <stdio.h>
  2. enum {
  3. ptChar,
  4. ptInt,
  5. ptFloat,
  6. ptDouble,
  7. };
  8. void printSum(unsigned long paramFormat, ...)
  9. {
  10. /*高16位为可变参数类型,低16位为可变参数个数*/
  11. int paramType = (paramFormat >> 16);
  12. int paramNum = paramFormat & 0xffff;
  13. /*¶mFormat = ebp + 8,第一个参数的地址*/
  14. unsigned long *pArg = ¶mFormat;
  15. /*ebp + 0x0c, 第二个参数地址*/
  16. pArg++;
  17. switch(paramType)
  18. {
  19. case ptChar:
  20. {
  21. int sum = 0;
  22. for (int i = 0; i < paramNum; i++)
  23. {
  24. char *pValue = (char *)pArg;
  25. sum += *pValue;
  26. pArg++;
  27. }
  28. printf("%d\n", sum);
  29. }
  30. break;
  31. case ptInt:
  32. {
  33. int sum = 0;
  34. for (int i = 0; i < paramNum; i++)
  35. {
  36. int *pValue = (int *)pArg;
  37. sum += *pValue;
  38. pArg++;
  39. }
  40. printf("%d\n", sum);
  41. }
  42. break;
  43. case ptFloat:
  44. {
  45. float sum = 0;
  46. /**/
  47. pArg++;
  48. /*浮点参数,堆栈占8个字节,所以指针偏移为8*/
  49. for (int i = 0; i < paramNum; i++)
  50. {
  51. float *pValue = (float *)pArg;
  52. sum += *pValue;
  53. pArg++;
  54. pArg++;
  55. }
  56. printf("%f\n", sum);
  57. }
  58. break;
  59. case ptDouble:
  60. {
  61. double sum = 0;
  62. /*双精度浮点参数,堆栈占8个字节,所以指针偏移为8*/
  63. for (int i = 0; i < paramNum; i++)
  64. {
  65. double *pValue = (double *)pArg;
  66. sum += *pValue;
  67. pArg++;
  68. pArg++;
  69. }
  70. printf("%f\n", sum);
  71. }
  72. break;
  73. default:
  74. printf("unknowned type!\n");
  75. break;
  76. }
  77. }
  78. void main()
  79. {
  80. unsigned long paramFormat = 3;
  81. char a = 1, b = 2, c = 3;
  82. printSum(paramFormat, a, b, c);
  83. paramFormat = ptInt << 16;
  84. paramFormat += 3;
  85. int ia = 1, ib = 2, ic = 3;
  86. printSum(paramFormat, ia, ib, ic);
  87. paramFormat = ptFloat << 16;
  88. paramFormat += 3;
  89. float fa = 1, fb = 2, fc = 3;
  90. printSum(paramFormat, fa, fb, fc);
  91. paramFormat = ptDouble << 16;
  92. paramFormat += 3;
  93. double da = 1, db = 2, dc = 3;
  94. printSum(paramFormat, da, db, dc);
  95. }

上面这种方法对函数参数的入栈顺序有限制,必须从右向左入栈,这就是为什么pascal调用方式不能实现printf的原因,并且函数形参都要通过栈来传递,这对有些编译器为了优化处理,函数参数通过寄存器来传递,从而不满足要求。鉴于次,本文采用C++的默认形参实现可变参数的方法,没有上面的这些限制,下面是实现代码:

  1. #include <stdio.h>
  2. enum {
  3. ptChar,
  4. ptInt,
  5. ptFloat,
  6. ptDouble,
  7. };
  8. void printSum(unsigned long paramType,
  9. void *arg1 = NULL,
  10. void *arg2 = NULL,
  11. void *arg3 = NULL,
  12. void *arg4 = NULL,
  13. void *arg5 = NULL,
  14. void *arg6 = NULL,
  15. void *arg7 = NULL,
  16. void *arg8 = NULL,
  17. void *arg9 = NULL,
  18. void *arg10 = NULL)
  19. {
  20. void *arg[10] = {
  21. arg1,
  22. arg2,
  23. arg3,
  24. arg4,
  25. arg5,
  26. arg6,
  27. arg7,
  28. arg8,
  29. arg9,
  30. arg10,
  31. };
  32. switch(paramType)
  33. {
  34. case ptChar:
  35. {
  36. int sum = 0;
  37. for (int i = 0; i < 10; i++)
  38. {
  39. if (arg[i] != NULL)
  40. {
  41. char *pValue = (char *)arg[i];
  42. sum += *pValue;
  43. }
  44. else
  45. break;
  46. }
  47. printf("%d\n", sum);
  48. }
  49. break;
  50. case ptInt:
  51. {
  52. int sum = 0;
  53. for (int i = 0; i < 10; i++)
  54. {
  55. if (arg[i] != NULL)
  56. {
  57. int *pValue = (int *)arg[i];
  58. sum += *pValue;
  59. }
  60. else
  61. break;
  62. }
  63. printf("%d\n", sum);
  64. }
  65. break;
  66. case ptFloat:
  67. {
  68. float sum = 0;
  69. for (int i = 0; i < 10; i++)
  70. {
  71. if (arg[i] != NULL)
  72. {
  73. float *pValue = (float *)arg[i];
  74. sum += *pValue;
  75. }
  76. else
  77. break;
  78. }
  79. printf("%f\n", sum);
  80. }
  81. break;
  82. case ptDouble:
  83. {
  84. double sum = 0;
  85. for (int i = 0; i < 10; i++)
  86. {
  87. if (arg[i] != NULL)
  88. {
  89. double *pValue = (double *)arg[i];
  90. sum += *pValue;
  91. }
  92. else
  93. break;
  94. }
  95. printf("%f\n", sum);
  96. }
  97. break;
  98. default:
  99. printf("unknowned type!\n");
  100. break;
  101. }
  102. }
  103. void main()
  104. {
  105. unsigned long paramType = ptChar;
  106. char a = 1, b = 2, c = 3;
  107. printSum(paramType, &a, &b, &c);
  108. paramType = ptInt;
  109. int ia = 1, ib = 2, ic = 3;
  110. printSum(paramType, &ia, &ib, &ic);
  111. paramType = ptFloat;
  112. float fa = 1, fb = 2, fc = 3;
  113. printSum(paramType, &fa, &fb, &fc);
  114. paramType = ptDouble;
  115. double da = 1, db = 2, dc = 3;
  116. printSum(paramType, &da, &db, &dc);
  117. }

http://blog.csdn.net/rabinsong/article/details/8946514

C++可变参数的另一种实现的更多相关文章

  1. c 可变参数 定义可变参数的函数

    定义可变参数的函数,需要在stdarg.h头文件中定义的va_list类型和va_start.va_arg.va_end三个宏. 定义可变参数函数 va_list ap;  //实际是定义一个指针va ...

  2. 理解 Python 中的可变参数 *args 和 **kwargs:

    默认参数:  Python是支持可变参数的,最简单的方法莫过于使用默认参数,例如: def getSum(x,y=5): print "x:", x print "y:& ...

  3. Java 传递可变参数和方法重载

    形式:类型... 参数名 示例:public void show(int... a) {}; 可变参数在方法中被当作数组来处理 可变参数传值的四种方式: 一个值也不传,可变参数会接收到长度为0的数组 ...

  4. define可变参数,float数据传输

    define可变参数 一般在调试打印Debug信息的时候, 需要可变参数的宏. 从C99开始可以使编译器标准支持可变参数宏(variadic macros), 另外GCC也支持可变参数宏, 但是两种在 ...

  5. [11 Go语言基础-可变参数函数]

    [11 Go语言基础-可变参数函数] 可变参数函数 什么是可变参数函数 可变参数函数是一种参数个数可变的函数. 语法 如果函数最后一个参数被记作 ...T ,这时函数可以接受任意个 T 类型参数作为最 ...

  6. 嵌入式C语言自我修养 12:有一种宏,叫可变参数宏

    12.1 什么是可变参数宏 在上面的教程中,我们学会了变参函数的定义和使用,基本套路就是使用 va_list.va_start.va_end 等宏,去解析那些可变参数列表我们找到这些参数的存储地址后, ...

  7. 大数据学习day13------第三阶段----scala01-----函数式编程。scala以及IDEA的安装,变量的定义,条件表达式,for循环(守卫模式,推导式,可变参数以及三种遍历方式),方法定义,数组以及集合(可变和非可变),数组中常用的方法

    具体见第三阶段scala-day01中的文档(scala编程基础---基础语法)  1. 函数式编程(https://www.cnblogs.com/wchukai/p/5651185.html): ...

  8. C可变参数的函数

    我们实现一个简单的printf函数(可变参数) #include <stdio.h> #include <stdarg.h> void myprintf(const char ...

  9. 可变参数列表与printf()函数的实现

    问题 当我们刚开始学习C语言的时候,就接触到printf()函数,可是当时"道行"不深或许不够细心留意,又或者我们理所当然地认为库函数规定这样就是这样,没有发现这个函数与普通的函数 ...

随机推荐

  1. JVM学习之GC常用算法

    出处:博客园左潇龙的技术博客--http://www.cnblogs.com/zuoxiaolong,多谢分享 GC策略解决了哪些问题? 既然是要进行自动GC,那必然会有相应的策略,而这些策略解决了哪 ...

  2. js/jQuery实现复制到剪贴板功能,兼容所有浏览器

    因为工作的原因,需要实现这样一个功能:点击按钮,复制文本内容. 百度了一下,大都语焉不详,最终找到了一篇很好的博文,有讲解,有实例,捣鼓了一会,最终实现了功能. 网址在这里http://www.cnb ...

  3. Python的编码规范(PEP 8 & Google Python guide)

    PEP 8 Python 代码规范整理 click here Goole Python 风格指南 中文版 click here 大家有取舍的看吧. 因为文章不是原创的,所以只贴地址,给大家造成麻烦了, ...

  4. 发送邮件java实现

    下面代码可以实现普通qq邮箱发送邮件的功能,可以传附件,但是是固定的附件: 需要两个jar包:mail.jar,activation.jar mail.jar 下载地址: http://java.su ...

  5. J2SE知识点摘记(二十三)

    我们简单介绍一下这个接口: 1.4.3        Comparable 接口 在 java.lang 包中,Comparable 接口适用于一个类有自然顺序的时候.假定对象集合是同一类型,该接口允 ...

  6. J2SE知识点摘记(十四)

    1.        字符流 Reader是定义java的流式字符输入模式的抽象类,该类所有方法在出错的情况下都将引发IOException异常. Int read(char buffer[])     ...

  7. Xamarin.Android 如何使用Assets目录下的文件

    原文:Xamarin.Android 如何使用Assets目录下的文件 个人原创,转载注明出处:http://blog.csdn.net/supluo/article/details/43672411 ...

  8. jquery easyui combobox学习

    今天尝试了一下jquery easy ui的combobox,感觉蛮好用的,直接上代码吧 HTML如下 <div> <input id="btnBind" typ ...

  9. CSharp tar类型文件压缩与解压

    最近闲暇时间开始写点通用基础类在写到tar类型文件压缩与解压时遇到点问题 压缩用的类库我是下载的 SharpZipLib_0860版本 先上代码 加压核心 /// <summary> // ...

  10. dataGuard主备库角色切换

    切换顺序: 先主库后备库 --查看主库可切换状态: SQL> select switchover_status from v$database; SWITCHOVER_STATUS ------ ...