簡體   English   中英

釋放內存時出現分段錯誤

[英]segmentation fault while freeing memory

這是我使該程序具有分段錯誤的程序,我在free(somepath);倒數第二個代碼中在gdb中對其進行了檢查free(somepath); 我沒有任何理由為什么會出現此細分錯誤? 請有人提出一些建議。

#include<dirent.h>
#include<unistd.h>
#include<string.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<stdio.h>
char *directs[20], *files[20];
int i = 0;
int j = 0;
int count = 0;

void printdir(char *);
int count_dirs(char *);
int count_files(char *);
void new_printdir(int ,int ,char *);
int main()
{
    char startdir[20];
    printf("Scanning user directories\n");
    scanf("%s", startdir);
    printdir(startdir);
}

void printdir(char *dir)
{


    DIR *dp = opendir(dir);
    int nDirs, nFiles, nD, nF;

    nDirs = 0;
    nFiles = 0;
    nD = 0;
    nF = 0;
    if (dp) {
        struct dirent *entry = 0;
        struct stat statBuf;

        nDirs = count_dirs(dir);
        nFiles = count_files(dir);



  new_printdir(nDirs,nFiles,dir);


        while ((entry = readdir(dp)) != 0) {
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
                continue;
            }

    char *     filepath = malloc(strlen(dir) + strlen(entry->d_name) + 2);
            if (filepath) {
                sprintf(filepath, "%s/%s", dir, entry->d_name);
                if (lstat(filepath, &statBuf) == 0) {
                    if (S_ISDIR(statBuf.st_mode)) {
                    printdir(filepath);
                        }
                    else {
                    }
                }

            }    

            free(filepath);
        }        //2nd while

        closedir(dp);
    }

    else {
        fprintf(stderr, "Error, cannot open directory %s\n", dir);
    }

}                //printdir

int count_dirs(char *dir)
{
    DIR *dp = opendir(dir);
    int nD;
    nD = 0;
    if (dp) {
        struct dirent *entry = 0;
        struct stat statBuf;

        while ((entry = readdir(dp)) != 0) {
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
                continue;
            }

            char *filepath =  malloc(strlen(dir) + strlen(entry->d_name) + 2);

            if (filepath) {
                sprintf(filepath, "%s/%s", dir, entry->d_name);

                if (lstat(filepath, &statBuf) != 0) {
                    fprintf(stderr, "File Not found? %s\n",filepath);
                }

                if (S_ISDIR(statBuf.st_mode)) {
                    nD++;

                } else {
                    continue;
                }

                free(filepath);
            }
        }

        closedir(dp);
    } else {
        fprintf(stderr, "Error, cannot open directory %s\n", dir);
    }
    return nD;
}

int count_files(char *dir)
{
    DIR *dp = opendir(dir);
    int nF;
    nF = 0;
    if (dp) {
        struct dirent *entry = 0;
        struct stat statBuf;

        while ((entry = readdir(dp)) != 0) {
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
                continue;
            }

            char *filepath =
                malloc(strlen(dir) + strlen(entry->d_name) + 2);

            if (filepath) {
                sprintf(filepath, "%s/%s", dir, entry->d_name);

                if (lstat(filepath, &statBuf) != 0) {
                    fprintf(stderr, "File Not found? %s\n",    filepath);
                }

                if (S_ISDIR(statBuf.st_mode)) {

                    continue;
                } else {
                    nF++;

                }

                free(filepath);
            }
        }

        closedir(dp);
    } else {
        fprintf(stderr, "Error, cannot open file %s\n", dir);
    }
    return nF;
}

void new_printdir(int nDirs,int nFiles,char *dir)
{

      struct dirent **namelist;
        DIR *dop;
        int i, j,t,re,nD,nF;

        char userd[20],*somepath;

       i = scandir(dir, &namelist, 0, alphasort);
        t=0;

        if (i < 0)
                perror ("Scandir failed to open directory I hope you understand \n");
        else {
                for (j = 0; j < i; j++) {
                if(strcmp(".",namelist[j]->d_name)==0||strcmp("..",namelist[j]->d_name)==0)
                continue;
              somepath = malloc(strlen(dir)+strlen(namelist[j]->d_name)+2);
                  sprintf(somepath,"%s/%s",dir,namelist[j]->d_name);
                        dop=opendir(somepath);

                        if(dop)
                         {

                nD++;
                            if ((nDirs-nD)<3)    
                        {printf("%s ",namelist[j]->d_name);}
                          }
                     else {
                nF++;
                if ((nFiles-nF)<3)    
                printf("%s ",namelist[j]->d_name);
              }
            closedir(dop);
                        free(namelist[j]);
                }
        }
        free(namelist);
 free(somepath);

}

為什么會發生這種細分錯誤,這對我來說還不清楚。 我該怎么做才能擺脫它?

在代碼中,不能保證分配內存並將其分配給somepath (除了循環的最后一次迭代,您還無法釋放somepath)。 您應該將free(somepath)語句放在for循環的末尾,並且應該在函數的開頭和每個for循環的開始處都將somepath初始化為NULL,以避免類似的粗心錯誤。

您沒有初始化somepath變量-它具有未定義的值,除非malloc()執行(例如,用於空文件夾)。 您應該在定義位置將其初始化為NULL。

您無需將somepath初始化為0(NULL),但是在某些情況下可以釋放該值。 這意味着您釋放一個隨機值-這不是一個好主意。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM