[英]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. 我正在编写ac程序来从目录中读取文件和目录,然后指出在结构数据中找到的元素数量,并将动态数组中元素的名称指向同一结构的数据中。 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. 有趣的是,如果我只是将小于或等于2的值指向结构数据的动态数组,则错误消息不会显示出来。 Do you have any idea ?
你有什么主意吗 ?
Thank you! 谢谢!
You have 2 problems, that can be causing the SEGMENTATION FAULT 您有2个问题,可能导致SEGMENTATION FAULT
The n_files
field is a pointer, and you assigned an integer to it, it should be declared as n_files
字段是一个指针,你为它分配了一个整数,它应该被声明为
int n_files;
You don't ever allocate space for the file_name
field, you should at least provide a fixed size, like this 你没有为
file_name
字段分配空间,你应该至少提供一个固定的大小,就像这样
char *file_name[1000];
you could allocate the memory dynamically using malloc()
but that's another thing, and it requires explanation. 你可以使用
malloc()
动态分配内存,但这是另一回事,它需要解释。
note : enabling compiler warnigns would help you prevent silly mistakes like int *n_files
and then doing fd->n_files = n_files;
注意 :启用编译器warnigns将帮助您防止像
int *n_files
这样的愚蠢错误,然后执行fd->n_files = n_files;
. 。
n_files should not be a pointer n_files不应该是指针
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
尝试使用调试器查看结构
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.