为什么更改指向struct的指针后出现分段错误?
我目前有一个功能代码,当我试图把一个文件转换成数组来生成一个函数时,我得到了一个分段错误。我知道为什么更改指向struct的指针后出现分段错误?,c,file,pointers,segmentation-fault,C,File,Pointers,Segmentation Fault,我目前有一个功能代码,当我试图把一个文件转换成数组来生成一个函数时,我得到了一个分段错误。我知道fileToArray中的对象是正确的(就myData对象而言),因为在函数内部时,myData.length和myData.array都会正确返回。然而,指针在main中被引用之后,我得到一个seg错误。我是c新手,但所有这些都在没有指向结构的特定指针的情况下工作 因此,如果我用一个参数调用这个程序,该参数包含一个包含多行文本的文件,则会发生set错误 #include <stdio.h>
fileToArray
中的对象是正确的(就myData
对象而言),因为在函数内部时,myData.length
和myData.array
都会正确返回。然而,指针在main中被引用之后,我得到一个seg错误。我是c新手,但所有这些都在没有指向结构的特定指针的情况下工作
因此,如果我用一个参数调用这个程序,该参数包含一个包含多行文本的文件,则会发生set错误
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
typedef struct {
int length;
char** array;
} FileStruct;
void fileToArray(FileStruct* fileDataPtr, int argc, char *argv[]){
int fd, i, n, count;
struct stat statbuf;
char *buf, *inbuf, *str, *saveptr;
char **array;
if ((fd = open(argv[1], O_RDONLY)) == -1) {
printf("Error opening file %s\n", argv[1]);
exit (-1);
}
if (lstat(argv[1], &statbuf) < 0) {
printf("Unable to lstat file %s\n", argv[1]);
exit (-1);
}
off_t filesize = statbuf.st_size;
buf = malloc(sizeof(char)*filesize);
array = malloc(sizeof(char *)*filesize);
count = 0;
if ((n = read(fd, buf, filesize)) > 0){
inbuf = buf;
for (i = 1; ; inbuf = NULL, i++) {
str = strtok_r(inbuf, "\n", &saveptr);
if (str == NULL)
break;
array[count] = malloc(sizeof(char)*(strlen(str)+1));
strcpy(array[count++], str);
}
} else {
printf("Error reading input file\n");
exit (-1);
}
close(fd);
// I know array works because it prints correctly here.
for (i = 0; i < count; i++) {
printf("%s\n", array[i]);
free(array[i]);
}
fileDataPtr->length = count;
fileDataPtr->array = array;
free(array);
free(buf);
}
int main(int argc, char *argv[]) {
int i;
FileStruct myData;
FileStruct* fileDataPtr = &myData;
fileToArray(fileDataPtr, argc, argv);
printf("length: %i", myData.length);
// I know this doesn't work because anything related to myData causes Seg fault.
// for (i = 0; i < 1; i++) {
// printf("%s\n", myData.array[i]);
// free(myData.array[i]);
// }
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
类型定义结构{
整数长度;
字符**数组;
}文件结构;
void fileToArray(FileStruct*fileDataPtr,int argc,char*argv[]){
int fd,i,n,计数;
结构stat statbuf;
字符*buf、*inbuf、*str、*saveptr;
字符**数组;
如果((fd=open(argv[1],orduonly))=-1){
printf(“打开文件%s\n时出错”,argv[1]);
出口(-1);
}
if(lstat(argv[1],&statbuf)<0){
printf(“无法读取文件%s\n”,argv[1]);
出口(-1);
}
off_t filesize=statbuf.st_size;
buf=malloc(sizeof(char)*文件大小);
array=malloc(sizeof(char*)*filesize);
计数=0;
如果((n=read(fd,buf,filesize))>0){
inbuf=buf;
对于(i=1;inbuf=NULL,i++){
str=strtok_r(inbuf、\n、&saveptr);
如果(str==NULL)
打破
数组[count]=malloc(sizeof(char)*(strlen(str)+1);
strcpy(数组[count++],str);
}
}否则{
printf(“读取输入文件时出错”);
出口(-1);
}
关闭(fd);
//我知道数组可以工作,因为它在这里打印正确。
对于(i=0;ilength=count;
fileDataPtr->array=array;
自由(数组);
免费(buf);
}
int main(int argc,char*argv[]){
int i;
文件结构myData;
FileStruct*fileDataPtr=&myData;
fileToArray(fileDataPtr、argc、argv);
printf(“长度:%i”,myData.length);
//我知道这不起作用,因为任何与myData相关的内容都会导致Seg故障。
//对于(i=0;i<1;i++){
//printf(“%s\n”,myData.array[i]);
//free(myData.array[i]);
// }
返回0;
}
在fileToArray
的末尾附近,您将array
分配给fileDataPtr->array
,然后在下一行释放array
。这将使fileDataPtr->array
指向释放的内存(一个悬空指针)。当您稍后取消引用它时,您将进入未定义的行为,任何事情都可能发生
由于分配将分配内存的所有权转移到fileDataPtr
,因此从fileToArray
返回之前,不需要释放array
移除
空闲(数组)代码>行。使用调试器或分割征服-添加一些printf以缩小导致崩溃的实际行。嘿,谢谢。我已经添加了printf,这就是为什么我知道在我使用与myData
相关的任何东西之前,一切都是有效的。无论是打印myData.length
还是来自myData.array
的任何内容,我都会得到相同的错误。所以我知道错误就在那里。只是不知道如何解决。谢谢,我已经看了很久了!我讨厌它这么简单,但我想这是我最有可能忽视的事情,因为我是C的新手。