内存分配的Segmentaion故障

时间:2016-05-31 15:03:12

标签: c segmentation-fault

我有两个功能open_filesread_bytes。当我只调用open_files时,一切正常,但如果我之后调用read_bytes,我会在open_files中遇到分段错误。我使用gcc作为编译器。

open_files是一个查看目录并填充包含char数组的文件名和长度的结构的函数。

read_bytes是一个没有代码的函数,只返回1.

文件名的结构

struct file_name{
    char * name;
    int length;
};

主要功能:

int main(int argc, char **argv){
    struct file_name ** files;
    int file_length;
    unsigned char * hex;
    long int bytesLength;
    printf("CRAP");
    getchar();
    //Function open_files works if read_bytes function is not called....
    if(open_files(files, &file_length) >= 0){
        printf("CRAP2");
        getchar();
        if (read_bytes(hex, &bytesLength, files[0]->name) >= 0){
            for(int i = 0; i < bytesLength; ++i){
                printf("%X\n",hex[i]);
            }
        }
    }
    else{
        printf("Something went wrong");
    }

    printf("%s\n", "Helluuuuuu");

    free_memory(files, file_length);

    return 0;
}

open_files函数为目录中的每个文件创建一个file_name结构。

int open_files(struct file_name ** files, unsigned int * length){
    DIR * dir;
    struct dirent * ent;
    int count = 0;
    if((dir = opendir("TestFiles")) != NULL){

        while((ent = readdir(dir)) != NULL ){
            ++count;

        }
        count -= 2;
        closedir(dir);
    }
    else{
        //Couldn't open directory
        perror("");
        return -1;
    }
    printf("working");
    getchar();
    //Allocate memory
    *files = malloc(count * sizeof(struct file_name *));
    printf("not working");
    getchar();
    *length = count;

    if((dir = opendir("TestFiles")) != NULL){
        count = 0;
        while((ent = readdir(dir)) != NULL ){
            if(strcmp(ent->d_name,".") != 0 && strcmp(ent->d_name,"..") != 0){
                struct file_name * file = malloc(sizeof(struct file_name));
                file->name = malloc(strlen(ent->d_name));
                strcpy(file->name,ent->d_name);
                file->length = strlen(ent->d_name);
                files[count] = file;
                ++count;
            }
        }
        closedir(dir);
    }
    else{
        //Couldn't open directory
        perror("");
        return -1;
    }

    return 0;
}

read_bytes是一个返回1的空函数:

int read_bytes(unsigned char * hex, long int * length, char * file){
    //FILE * fp;
    //*length = file_size(file);
    //printf("%li\n",*length );

    //fp = fopen("TestFiles/first.jpg", "r");
    //fread(hex, 1, *length, fp);
    //fclose(fp);

    return 1;
}

1 个答案:

答案 0 :(得分:2)

你有

*files = malloc(count * sizeof(struct file_name *));

分配不正确的指针数组。结构指针数组的类型为struct file_name **,但您将其分配给struct file_name *·

然后你做:

files[count] = file;

对于覆盖count == 0malloc())返回的地址的files[0] == *files,对于count > 0,它会调用未定义的行为。

您可以将struct file_name ***传递给open_files()并更改

files[count] = file; 

(*files)[count] = file;

或分配一组结构:

 *files = malloc(count * sizeof(struct file_name) );  
 ....
        if(strcmp(ent->d_name,".") != 0 && strcmp(ent->d_name,"..") != 0){
            struct file_name * file = (*files) + count;  // struct is already allocated (alternatively: &((*files)[count])
            file->name = malloc(strlen(ent->d_name)+1);
            strcpy(file->name,ent->d_name);
            file->length = strlen(ent->d_name);
            ++count;
        }

main()中,您必须在两种情况下都将&files传递给open_files()。 如果您选择第二个解决方案,请更改

struct file_name ** files;

struct file_name * files;

files[0]->nam 

files[0].name

请注意file->name = malloc(strlen(ent->d_name)+1);,如前所述,这两种情况都是必要的