免费注册 查看新帖 |

Chinaunix

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

奇怪的问题,郁闷,在线等 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-11-06 21:10 |只看该作者 |倒序浏览
把sco的程序移植到linux上发现了个问题,不知道怎么解决

程序:

  1. #include  <stdio.h>

  2. void trimall(char** mystr)
  3. {
  4.         char *mytmpbuf;
  5.         int i;
  6.         mytmpbuf=*mystr;
  7.        
  8.         if(strlen(mytmpbuf)==0)
  9.                 return;
  10.         for(;(*mytmpbuf==' ')||(*mytmpbuf=='\t');mytmpbuf++);
  11.         i=strlen(mytmpbuf);

  12.         for(;(*(mytmpbuf+i)==' ')||(*(mytmpbuf+i)=='\n')||(*(mytmpbuf+i)=='\0');i--)
  13.         {
  14.                         *(mytmpbuf+i)=0;
  15.                 printf("[%s]",mytmpbuf+i);
  16.                 if (i==0)
  17.                         break;
  18.         }
  19.         *mystr = mytmpbuf;

  20. }

  21. int StrTrim(s)
  22. char *s;
  23. {
  24.         short        i, l, r, len;

  25.         for(len=0; s[len]; len++);
  26.         for(l=0; (s[l]==' ' || s[l]=='\t' || s[l]=='\n'); l++);
  27.         if(l==len)
  28.         {
  29.                 s[0]='\0';
  30.                 return(0);
  31.         }
  32.         for(r=len-1; ((s[r]==' ' )|| (s[r]=='\t' )||( s[r]=='\n'));r--);
  33.         for(i=l;i<=r;i++)  
  34.           s[i-l]=s[i];
  35.         s[r-l+1]='\0';
  36.         return(0);
  37. }

  38. char * FunSep(char *alParam1,short ilOrder)
  39. {
  40.   StrTrim(alParam1);
  41.   return;
  42. }

  43. int main()
  44. {
  45.         char tmpbuf1[2048];
  46.         char* tmpbuf2;
  47.         short tmpbuf1len;
  48.         memcpy(tmpbuf1,"tmpbuf1",7);
  49.         tmpbuf1[7]=0;
  50.         fprintf(stderr,"\n tmpbuf1=[%s]\n",tmpbuf1);
  51.         [b]FunSep(tmpbuf1,1);[/b]
  52.         fprintf(stderr,"\n tmpbuf1=[%s]\n",tmpbuf1);
  53.         printf("aaa");

  54.        

  55.         return 0;
  56. }
复制代码

一个测试的程序,发现居然把FunSep(tmpbuf1,1);改为FunSep("tmpbuf1",1)时编译通过,但执行居然core掉了,
想了半天没明白。另外在在sco上测试是正常的,但在suse上居然core了,why,
我就用了简单的gcc -g -o编的

我用gdb跟了下,把跟踪发上去
用FunSep("tmpbuf1",1)时:

  1. (gdb) break 58
  2. Breakpoint 1 at 0x8048670: file mytest.c, line 58.
  3. (gdb) break 46
  4. Breakpoint 2 at 0x8048604: file mytest.c, line 46.
  5. (gdb) break 31
  6. Breakpoint 3 at 0x8048512: file mytest.c, line 31.
  7. (gdb) r
  8. Starting program: /home/proj/tmp/mytest

  9. tmpbuf1=[tmpbuf1]

  10. Breakpoint 1, main () at mytest.c:58
  11. 58              mytesttest("tmpbuf1",1);
  12. (gdb) n

  13. Breakpoint 2, mytesttest (str=0x8048842 "tmpbuf1", i=1) at mytest.c:46
  14. 46              StrTrim(str);
  15. (gdb) p str
  16. $1 = 0x8048842 "tmpbuf1"
  17. (gdb) n

  18. Breakpoint 3, StrTrim (s=0x8048842 "tmpbuf1") at mytest.c:31
  19. 31              for(l=0; (s[l]==' ' || s[l]=='\t' || s[l]=='\n'); l++);
  20. (gdb) n
  21. 32              if(l==len)
  22. (gdb) p s
  23. $2 = 0x8048842 "tmpbuf1"
  24. (gdb) n
  25. 37              for(r=len-1; ((s[r]==' ' )|| (s[r]=='\t' )||( s[r]=='\n'));r--);
  26. (gdb) n
  27. 38              for(i=l;i<=r;i++)
  28. (gdb) n
  29. 39                s[i-l]=s[i];
  30. (gdb) n

  31. Program received signal SIGSEGV, Segmentation fault.
  32. 0x080485c9 in StrTrim (s=0x8048842 "tmpbuf1") at mytest.c:39
  33. 39                s[i-l]=s[i];
  34. (gdb)
复制代码






用FunSep(tmpbuf1,1)时正常

  1. (gdb) break 58
  2. Breakpoint 1 at 0x8048670: file mytest.c, line 58.
  3. (gdb) break 46
  4. Breakpoint 2 at 0x8048604: file mytest.c, line 46.
  5. (gdb) break 31
  6. Breakpoint 3 at 0x8048512: file mytest.c, line 31.
  7. (gdb) r
  8. Starting program: /home/proj/tmp/mytest

  9. tmpbuf1=[tmpbuf1]

  10. Breakpoint 1, main () at mytest.c:58
  11. 58              FunSep(tmpbuf1,1);
  12. (gdb) n

  13. Breakpoint 2, FunSep (alParam1=0xbfffe9e0 "tmpbuf1", ilOrder=1) at mytest.c:46
  14. 46        StrTrim(str);
  15. (gdb) p str
  16. $1 = 0xbfffe9e0 "tmpbuf1"
  17. (gdb) n

  18. Breakpoint 3, StrTrim (s=0xbfffe9e0 "tmpbuf1") at mytest.c:31
  19. 31              for(l=0; (s[l]==' ' || s[l]=='\t' || s[l]=='\n'); l++);
  20. (gdb) n
  21. 32              if(l==len)
  22. (gdb) p s
  23. $2 = 0xbfffe9e0 "tmpbuf1"
  24. (gdb) n
  25. 37              for(r=len-1; ((s[r]==' ' )|| (s[r]=='\t' )||( s[r]=='\n'));r--);
  26. (gdb) n
  27. 38              for(i=l;i<=r;i++)
  28. (gdb) n
  29. 39                s[i-l]=s[i];
  30. (gdb) n
  31. 38              for(i=l;i<=r;i++)
  32. (gdb) n
  33. 39                s[i-l]=s[i];
  34. (gdb) n
  35. 38              for(i=l;i<=r;i++)
  36. (gdb) n
  37. 39                s[i-l]=s[i];
  38. (gdb)
复制代码

[ 本帖最后由 vveinv 于 2006-11-6 21:57 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-11-06 21:48 |只看该作者
如果你的程序里没有错误的话,
你要做的就和

  1. int main()
  2. {
  3.         char* buffer = "This is a SEGV error!,because string literal is in readonly area.";
  4.         *buffer = 't';
  5.         return 0;
  6. }
复制代码

所起的作用是一样的,都是在试图修改常量存储区.

看看刚才贴出的那个c语言四书五经的帖子,就看一下那本c和指针吧.

贴代码时,用code围起来,否则会吃掉某些字符.

记得flw以前谢过一个trim的实现,你在精华区找一下吧.

s[i-l]=s;这样的代码肯定不对.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP