ошибка сегментации при освобождении памяти - PullRequest
0 голосов
/ 27 декабря 2010

Вот программа, которую я сделал, эта программа имеет ошибку сегментации. Я проверил ее в 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);

}

Почему происходит ошибка сегментации, мне не ясно.Что я могу сделать, чтобы избавиться от этого?

Ответы [ 3 ]

3 голосов
/ 27 декабря 2010

В вашем коде вам не гарантируется выделение памяти и ее присвоение somepath (вам также не удается освободить somepath, за исключением последней итерации цикла).Вы должны поместить свой оператор free(somepath) в конец цикла for, и вы должны инициализировать somepath в NULL как в самом начале функции, так и в начале каждого цикла for, чтобы избежать подобных небрежных ошибок.

2 голосов
/ 27 декабря 2010

Вы не инициализируете переменную somepath - она ​​имеет неопределенное значение, если только не выполняется malloc () (т.е. для пустых папок). Вы должны инициализировать его в NULL в месте определения.

1 голос
/ 27 декабря 2010

Вы не инициализируете somepath равным 0 (NULL), но при некоторых обстоятельствах можете сбросить значение.Это означает, что вы выпускаете случайное значение - что не очень хорошая идея.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...