免费注册 查看新帖 |

Chinaunix

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

请教变参问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-13 18:24 |只看该作者 |倒序浏览
following are codes:

  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <sys/stat.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>

  7. void t_var_argu(char * fmt,...);

  8. int main(void)
  9. {
  10.     printf("test variable parameters.\n\n");
  11.    
  12.     char *env;
  13.     env=getenv("PATH");
  14.     if(env==NULL)
  15.     {   
  16.         t_var_argu("current pid:%d,get the environment failure.\n",getpid());
  17.         return -1;
  18.     }
  19.     sleep(5);
  20.     t_var_argu("current pid:%d,get the environment:%s success.\n",getpid(),env);
  21.    
  22.     return 0;
  23. }

  24. void t_var_argu(char * fmt,...)
  25. {
  26.     va_list arg_ptr;
  27.     char * message;
  28.     va_start(arg_ptr,fmt);    /*to direct a variable argument type point */
  29.     vsprintf(message,fmt,arg_ptr); /* convert to string and copy into message */
  30.     printf("%s",message);
  31. }   
复制代码

但是段错误,gdb使用还不会只知道断点,诚恳请教,谢谢。

论坛徽章:
0
2 [报告]
发表于 2006-09-13 19:05 |只看该作者
越界了,
char * message = alloca( 1024 * strlen(fmt) + 1);


  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <sys/stat.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>

  7. void t_var_argu(char * fmt,...);

  8. int main(void)
  9. {
  10.         printf("test variable parameters.\n\n");

  11.         char *env;
  12.         env=getenv("PATH");
  13.         if(env==NULL)
  14.         {
  15.                 t_var_argu("current pid:%ld,get the environment failure.\n",getpid());
  16.                 return -1;
  17.         }
  18.         sleep(0.5);
  19.         t_var_argu("current pid:%ld,get the environment:%s success.\n",getpid(),env);

  20.         return 0;
  21. }

  22. void t_var_argu(char * fmt,...)
  23. {
  24.         va_list arg_ptr;
  25.         [color=Red]char * message = alloca( 1024 * strlen(fmt) + 1);[/color]
  26.         va_start(arg_ptr,fmt);    /*to direct a variable argument type point */
  27.         vsprintf(message,fmt,arg_ptr); /* convert to string and copy into message */
  28.         va_end(arg_ptr);
  29.         printf("%s",message);
  30. }

复制代码

[ 本帖最后由 lonelyair 于 2006-9-13 19:07 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2006-09-13 19:34 |只看该作者
原帖由 lonelyair 于 2006-9-13 19:05 发表
越界了,
char * message = alloca( 1024 * strlen(fmt) + 1);
[_code]

#include <stdio.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <sys/types.h&g ...


在栈上分配和在堆上分配相比有哪些好处?

论坛徽章:
0
4 [报告]
发表于 2006-09-14 09:25 |只看该作者
先多谢一下,另,刚刚看到alloca()不利于或不具移植性,在写程序的时候经常用到它吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP