简体   繁体   中英

C - Pointer to dynamic array in struct “Segmentation fault (core dumped)”

I'm writing ac program to read the files and directories from a directory and then point the number of elements found in a data of a struct and point the name of the elements in a dynamic array in a data of the same struct. I did it and its output is right. The problem is that when I run the program a "Segmentation fault (core dumped)" shows up.

The code:

#include <stdio.h>
#include <dirent.h>

#define EXIT_FAILURE 1

typedef struct FileDir
{   
    int *n_files;
    char *file_name[];
} FileDir;

int get_files_within_dir(struct FileDir *fd)
{
    DIR *dir;
    int n_files;
    int index;

    n_files = 0;
  if ((dir = opendir ("/tmp")) != NULL) {
    /* counts all the files and directories within directory */
    while (readdir (dir) != NULL) {
      n_files++;
    }
    closedir (dir);
  } else {
    /* could not open directory */
    perror ("");
    return EXIT_FAILURE;
  }       

  char *file_name[n_files];
  struct dirent *ent;
  if ((dir = opendir ("/tmp")) != NULL) {
    /* gets all the files and directories within directory */
    index = 0;
    while ((ent = readdir (dir)) != NULL) {
      file_name[index++] = ent->d_name;      
    }
    closedir (dir);
  } else {
    /* could not open directory */
    perror ("");
    return EXIT_FAILURE;
  }      

  fd->n_files = n_files;  
  fd->file_name[0] = file_name[0];
  fd->file_name[1] = file_name[1];
  fd->file_name[2] = file_name[2];
  fd->file_name[3] = file_name[3];

  return 0;  
}

int main()
{   
    struct FileDir fd;
    get_files_within_dir(&fd);
    printf("%d\n", fd.n_files);
    printf("%s\n", fd.file_name[1]);
    printf("%s\n", fd.file_name[2]);
    printf("%s\n", fd.file_name[3]);    

    return 0;
}

The output:

[freitas@localhost src]$ ./file_dir 
21
..
geany_socket.fcda02b3
tmpiSdUX3
Segmentation fault (core dumped)

The interesting thing is that if I just point less than or equal to 2 values to the dynamic array of the data of the struct the error message does not show up. Do you have any idea ?

Thank you!

You have 2 problems, that can be causing the SEGMENTATION FAULT

  1. The n_files field is a pointer, and you assigned an integer to it, it should be declared as

     int n_files; 
  2. You don't ever allocate space for the file_name field, you should at least provide a fixed size, like this

     char *file_name[1000]; 

    you could allocate the memory dynamically using malloc() but that's another thing, and it requires explanation.

note : enabling compiler warnigns would help you prevent silly mistakes like int *n_files and then doing fd->n_files = n_files; .

n_files should not be a pointer

typedef struct FileDir
{   
 int n_files;
 char *file_name[];
} FileDir;

Then your line

 printf("%d\n", fd.n_files);

will not crash. Try looking at the struct with a debugger

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM