免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3136 | 回复: 1

[C] 出现段错误了,怎么办? [复制链接]

论坛徽章:
0
发表于 2013-12-27 18:45 |显示全部楼层
结果输出正常,但最后一行出现段错误,用strace跟踪发现一下错误:
4709  write(1, "who2_24  509 rwxrwxr-x\n", 23) = 23
4709  stat64("/home/spring/c_workspace/dec_13/upp/ch2/precious_time", {st_mode=S_IFDIR|0775, st_size=4096, ...}) = 0
4709  write(1, "precious_time  509 rwxrwxr-x\n", 29) = 29
4709  getdents(3, /* 0 entries */, 3276 = 0
4709  --- SIGSEGV (Segmentation fault) @ 0 (0) ---
4709  +++ killed by SIGSEGV +++

程序如下:
/* ls1_27.c - a sort of program implementing the utility 'ls'
* features:list complicated details about a directory set by users.
* usage:
* building:gcc ls1_27.c -lm -o ls1_27
*/
#include<stdio.h>
#include<dirent.h>
#include<string.h>
#include<sys/types.h>
#include<math.h>
#include<sys/stat.h>
#define LEN 64
#define MSIZE 9
void print_mode(mode_t m)
{
        int mode[MSIZE];
        int i=MSIZE-1,j;
        for(;i>=0;i--)
        {
                j=MSIZE-i-1;
                mode[j]=m/pow(2,i);
                m=m%(int)pow(2,i);
                if(j%3==0)
                {
                        if(mode[j]==1)printf("r";
                        else printf("-";
                }
                else if(j%3==1)
                {
                        if(mode[j]==1)printf("w";
                        else printf("-";
                }
                else
                {
                        if(mode[j]==1)printf("x";
                        else printf("-";
                }
        }
        printf("\n";
}
int main(int argc,char * argv[])
{
        const char * ccPathName="/home/spring/c_workspace/dec_13/upp/ch2";
        char ccPathName2[LEN];
        DIR * DD=opendir(ccPathName);
        struct dirent stDir,*pstDir;
        printf(ccPathName);
        printf(":\n";
        while((pstDir=readdir(DD))!=NULL)
        {
                printf("%s  ",pstDir->d_name);
                memmove(ccPathName2,ccPathName,strlen(ccPathName));
                strcat(ccPathName2,"/";
                strcat(ccPathName2,pstDir->d_name);
                struct stat * ssBuf;
                stat(ccPathName2,ssBuf);
                mode_t st_mode2=(ssBuf->st_mode&0777);
                printf("%d ",st_mode2);
                print_mode(st_mode2);
                memset(ccPathName2,0,sizeof(ccPathName2));
        }
        close(DD);
        return 0;
}

论坛徽章:
1
亥猪
日期:2014-09-10 11:43:17
发表于 2013-12-27 20:20 |显示全部楼层
呵呵,你写错了能怎么办,改呗。

stat(ccPathName2,ssBuf);

问题在这里,stat的第二个参数要求的是一个指向实体的指针,而你只传给它一个未初始化的悬浮指针,当然会导致内存读写错误。

memmove(ccPathName2,ccPathName,strlen(ccPathName));

这句也有问题,字符串的复制为什么不用strcpy,而用这个?你少复制了字符串的结束符。这也是你在循环最后加了个memset的原因吧?还是存在风险。

print_mode中不用移位操作却用了个数学函数pow(2,i)。

呵呵,改了改你的代码给你对比。
  1. #include<stdio.h>
  2. #include<dirent.h>
  3. #include<string.h>
  4. #include<sys/types.h>
  5. #include<sys/stat.h>
  6. #define LEN 512
  7. void print_mode(mode_t m)
  8. {
  9.         char *s = "xwr";
  10.         int i;
  11.         for(i = 8; i >= 0; i--)
  12.                 putchar(m & (1 << i) ? s[i%3] : '-');
  13.         printf("\n");
  14. }
  15. int main()
  16. {
  17.         const char * ccPathName = "/home/yf/c";
  18.         char ccPathName2[LEN];
  19.         DIR * DD = opendir(ccPathName);
  20.         struct dirent *pstDir;

  21.         printf("%s:\n", ccPathName);
  22.         while((pstDir=readdir(DD))!=NULL)
  23.         {
  24.                 printf("%-16s\t", pstDir->d_name);
  25.                 sprintf(ccPathName2, "%s/%s", ccPathName, pstDir->d_name);
  26.                 struct stat ssBuf;
  27.                 stat(ccPathName2,&ssBuf);
  28.                 mode_t st_mode2=(ssBuf.st_mode&0777);
  29.                 printf("%o ",st_mode2);
  30.                 print_mode(st_mode2);
  31.         }
  32.         close(DD);
  33.         return 0;
  34. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP