免费注册 查看新帖 |

Chinaunix

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

sco移植到suse上发现函数入参的问题 [复制链接]

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

程序:
#include  <stdio.h>

void trimall(char** mystr)
{
        char *mytmpbuf;
        int i;
        mytmpbuf=*mystr;
       
        if(strlen(mytmpbuf)==0)
                return;
        for(;(*mytmpbuf==' ')||(*mytmpbuf=='\t');mytmpbuf++);
        i=strlen(mytmpbuf);

        for(;(*(mytmpbuf+i)==' ')||(*(mytmpbuf+i)=='\n')||(*(mytmpbuf+i)=='\0');i--)
        {
                        *(mytmpbuf+i)=0;
                printf("[%s]",mytmpbuf+i);
                if (i==0)
                        break;
        }
        *mystr = mytmpbuf;

}

int StrTrim(s)
char *s;
{
        short        i, l, r, len;

        for(len=0; s[len]; len++);
        for(l=0; (s[l]==' ' || s[l]=='\t' || s[l]=='\n'); l++);
        if(l==len)
        {
                s[0]='\0';
                return(0);
        }
        for(r=len-1; ((s[r]==' ' )|| (s[r]=='\t' )||( s[r]=='\n'));r--);
        for(i=l;i<=r;i++)  
          s[i-l]=s;
        s[r-l+1]='\0';
        return(0);
}

char * FunSep(char *alParam1,short ilOrder)
{
  StrTrim(alParam1);
  return;
}

int main()
{
        char tmpbuf1[2048];
        char* tmpbuf2;
        short tmpbuf1len;
        memcpy(tmpbuf1,"tmpbuf1",7);
        tmpbuf1[7]=0;
        fprintf(stderr,"\n tmpbuf1=[%s]\n",tmpbuf1);
        FunSep(tmpbuf1,1);
        fprintf(stderr,"\n tmpbuf1=[%s]\n",tmpbuf1);
        printf("aaa";

       

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

我用gdb跟了下,把跟踪发上去
用FunSep("tmpbuf1",1)时:
(gdb) break 58
Breakpoint 1 at 0x8048670: file mytest.c, line 58.
(gdb) break 46
Breakpoint 2 at 0x8048604: file mytest.c, line 46.
(gdb) break 31
Breakpoint 3 at 0x8048512: file mytest.c, line 31.
(gdb) r
Starting program: /home/proj/tmp/mytest

tmpbuf1=[tmpbuf1]

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

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

Breakpoint 3, StrTrim (s=0x8048842 "tmpbuf1" at mytest.c:31
31              for(l=0; (s[l]==' ' || s[l]=='\t' || s[l]=='\n'); l++);
(gdb) n
32              if(l==len)
(gdb) p s
$2 = 0x8048842 "tmpbuf1"
(gdb) n
37              for(r=len-1; ((s[r]==' ' )|| (s[r]=='\t' )||( s[r]=='\n'));r--);
(gdb) n
38              for(i=l;i<=r;i++)
(gdb) n
39                s[i-l]=s;
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
0x080485c9 in StrTrim (s=0x8048842 "tmpbuf1" at mytest.c:39
39                s[i-l]=s;
(gdb)





用FunSep(tmpbuf1,1)时正常
(gdb) break 58
Breakpoint 1 at 0x8048670: file mytest.c, line 58.
(gdb) break 46
Breakpoint 2 at 0x8048604: file mytest.c, line 46.
(gdb) break 31
Breakpoint 3 at 0x8048512: file mytest.c, line 31.
(gdb) r
Starting program: /home/proj/tmp/mytest

tmpbuf1=[tmpbuf1]

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

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

Breakpoint 3, StrTrim (s=0xbfffe9e0 "tmpbuf1" at mytest.c:31
31              for(l=0; (s[l]==' ' || s[l]=='\t' || s[l]=='\n'); l++);
(gdb) n
32              if(l==len)
(gdb) p s
$2 = 0xbfffe9e0 "tmpbuf1"
(gdb) n
37              for(r=len-1; ((s[r]==' ' )|| (s[r]=='\t' )||( s[r]=='\n'));r--);
(gdb) n
38              for(i=l;i<=r;i++)
(gdb) n
39                s[i-l]=s;
(gdb) n
38              for(i=l;i<=r;i++)
(gdb) n
39                s[i-l]=s;
(gdb) n
38              for(i=l;i<=r;i++)
(gdb) n
39                s[i-l]=s;
(gdb)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP