免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 5213 | 回复: 25
打印 上一主题 下一主题

[C] 不定实参调用问题(已解决) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-15 10:20 |只看该作者 |正序浏览
hp-ux机器

  1. #include <stdio.h>

  2. void fun(int a, ...);

  3. int main(int argc, char *argv[])
  4. {
  5.   int a = 1;
  6.   int b = 2;
  7.   int c = 3;
  8.   int d = 4;
  9.   fun(4, a, b, c, d);
  10.   return 0;
  11. }

  12. void fun(int a, ...)
  13. {
  14.   int *temp = &a;
  15.   temp++;
  16.   for(int i = 0;i < a; ++i)
  17.   {
  18.     printf("%d ", *temp);
  19.     temp++;
  20.   }
  21.   printf("\n");
  22. }
复制代码


运行结果:
0 1 0 2

预计运行结果:
0 1 2 3
为什么?大侠指点

[ 本帖最后由 ZSMDEV 于 2009-12-15 14:41 编辑 ]

论坛徽章:
0
26 [报告]
发表于 2009-12-16 13:35 |只看该作者
原帖由 ZSMDEV 于 2009-12-15 10:20 发表
hp-ux机器

#include

void fun(int a, ...);

int main(int argc, char *argv[])
{
  int a = 1;
  int b = 2;
  int c = 3;
  int d = 4;
  fun(4, a, b, c, d);
  return 0;
}

void fun(i ...


假如你的 hp-ux 是运行在64bit的 Itanium 上。而编译器按32bit指针,
就有可能出现你得到的结果吧

论坛徽章:
0
25 [报告]
发表于 2009-12-15 14:21 |只看该作者
原帖由 ZSMDEV 于 2009-12-15 14:17 发表

好,大师,怎么结贴

标题里面加个“已解决”吧

论坛徽章:
0
24 [报告]
发表于 2009-12-15 14:17 |只看该作者
原帖由 zx_wing 于 2009-12-15 12:43 发表

建议你结贴,现在是一群人在以IA32的calling convention来讨论你的程序
而IA64的calling convention和IA32完全不一样。建议你查阅IA64的calling convention,了解函数传参的相关类容,靠猜是没有用的

好,大师,怎么结贴

论坛徽章:
0
23 [报告]
发表于 2009-12-15 13:12 |只看该作者
原帖由 zx_wing 于 2009-12-15 12:43 发表

建议你结贴,现在是一群人在以IA32的calling convention来讨论你的程序
而IA64的calling convention和IA32完全不一样。建议你查阅IA64的calling convention,了解函数传参的相关类容,靠猜是没有用的

好,大师
顺便告诉我,怎么结贴

论坛徽章:
0
22 [报告]
发表于 2009-12-15 12:50 |只看该作者
原帖由 ZSMDEV 于 2009-12-15 11:50 发表

我觉得打印的不是随机内容,
4个数的时候,打印0 1 0 2
6个数的时候,打印0 1 0 2 0 3
是按照参数顺序,但是把高低位分开了

我给你贴个链接吧

http://www.csee.umbc.edu/help/architecture/24525601.pdf

参考8.5节 《Parameter Passing》

论坛徽章:
0
21 [报告]
发表于 2009-12-15 12:44 |只看该作者
原帖由 gtkmm 于 2009-12-15 11:55 发表
不是intel汇编啊,好多寄存器,我晕了。。

这是INTEL的汇编,只是不是大家熟悉的IA32架构而已

论坛徽章:
0
20 [报告]
发表于 2009-12-15 12:43 |只看该作者
原帖由 ZSMDEV 于 2009-12-15 11:50 发表

我觉得打印的不是随机内容,
4个数的时候,打印0 1 0 2
6个数的时候,打印0 1 0 2 0 3
是按照参数顺序,但是把高低位分开了

建议你结贴,现在是一群人在以IA32的calling convention来讨论你的程序
而IA64的calling convention和IA32完全不一样。建议你查阅IA64的calling convention,了解函数传参的相关类容,靠猜是没有用的

论坛徽章:
0
19 [报告]
发表于 2009-12-15 12:03 |只看该作者
#include <stdio.h>

void fun(int a, ...);

int main(int argc, char *argv[])
{
&nbsp;&nbsp;int a = 1;
&nbsp;&nbsp;int b = 2;
&nbsp;&nbsp;int c = 3;
&nbsp;&nbsp;int d = 4;
&nbsp;&nbsp;fun(4, a, b, c, d);
&nbsp;&nbsp;return 0;
}

void fun(int a, ...)
{
&nbsp;&nbsp;int *tmp = &a;
&nbsp;&nbsp;tmp++;
&nbsp;&nbsp;long long *temp = tmp;
&nbsp;&nbsp;for(int i = 0;i < a; ++i)
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;printf("%d ", *temp);
&nbsp;&nbsp;&nbsp;&nbsp;temp++;
&nbsp;&nbsp;}
&nbsp;&nbsp;printf("n");
}

不知是不是按这样写的?

论坛徽章:
0
18 [报告]
发表于 2009-12-15 11:55 |只看该作者
不是intel汇编啊,好多寄存器,我晕了。。
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP