亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪(fǎng)問(wèn)板塊 發(fā)新帖
查看: 3188 | 回復(fù): 1
打印 上一主題 下一主題

[C] 出現(xiàn)段錯(cuò)誤了,怎么辦? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2013-12-27 18:45 |只看該作者 |倒序?yàn)g覽
結(jié)果輸出正常,但最后一行出現(xiàn)段錯(cuò)誤,用strace跟蹤發(fā)現(xiàn)一下錯(cuò)誤:
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
2 [報(bào)告]
發(fā)表于 2013-12-27 20:20 |只看該作者
呵呵,你寫(xiě)錯(cuò)了能怎么辦,改唄。

stat(ccPathName2,ssBuf);

問(wèn)題在這里,stat的第二個(gè)參數(shù)要求的是一個(gè)指向?qū)嶓w的指針,而你只傳給它一個(gè)未初始化的懸浮指針,當(dāng)然會(huì)導(dǎo)致內(nèi)存讀寫(xiě)錯(cuò)誤。

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

這句也有問(wèn)題,字符串的復(fù)制為什么不用strcpy,而用這個(gè)?你少?gòu)?fù)制了字符串的結(jié)束符。這也是你在循環(huán)最后加了個(gè)memset的原因吧?還是存在風(fēng)險(xiǎn)。

print_mode中不用移位操作卻用了個(gè)數(shù)學(xué)函數(shù)pow(2,i)。

呵呵,改了改你的代碼給你對(duì)比。
  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. }
復(fù)制代碼
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專(zhuān)區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP