C 分段错误-文件写入
我在写入由变量fp2表示的parent2.txt文件时遇到分段错误。我把注释放在程序停止运行的那一行和负责这个问题的那一行。我猜分割错误是temp4.value设置为null的结果,尽管我认为我给了它一个值。行temp2=*temp2.next似乎有问题。temp2是子结构的一个变量。我不知道代码到底出了什么问题,这正是我需要帮助的地方。这是我的密码:C 分段错误-文件写入,c,file-io,gdb,C,File Io,Gdb,我在写入由变量fp2表示的parent2.txt文件时遇到分段错误。我把注释放在程序停止运行的那一行和负责这个问题的那一行。我猜分割错误是temp4.value设置为null的结果,尽管我认为我给了它一个值。行temp2=*temp2.next似乎有问题。temp2是子结构的一个变量。我不知道代码到底出了什么问题,这正是我需要帮助的地方。这是我的密码: #include <stdio.h> #include <stdlib.h> #include <errno.h&
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
typedef struct Parent
{
char *value;
struct Child *next;
int numChildren;
}Parent;
typedef struct Child
{
char *value;
struct Child *next;
Parent *prev1;
struct Child *prev2;
}Child;
int isFirst(int parent[], int index)
{
int i;
for (i = 0; i < index; i++)
{
if (parent[i] == parent[index])
{
return 0;
}
}
return 1;
}
int addChild(Parent parents[], int index, char *num)
{
int i;
Child temp, temp2;
temp.value = num;
if (parents[index].numChildren == 0)
{
parents[index].next = &temp;
temp.prev1 = &parents[index];
temp.prev2 = NULL;
}
else
{
for (i = 0; i < parents[index].numChildren; i++)
{
if (i == 0)
{
temp2 = *parents[index].next;
}
else
{
temp2 = *temp2.next; // setting temp2 to null?
}
}
temp.prev1 = NULL;
temp.prev2 = &temp2;
temp2.next = &temp;
}
}
int main() {
FILE *fp, *fp2;
char read_file [256];
char current[256];
char space = ' ';
char *curr2, *curr3;
int i, index;
int j = 0;
int parent[] = {1, 1, 1, 3, 3, 4, 4, 1};
int child[] = {3, 4, 5, 7, 8, 10, 11, 100};
Parent parents[10];
Parent temp;
Child temp2, temp3;
int numParents = 0;
int done = 0;
fp = fopen ("/home/sam/parent.txt","w+");
if (fp == NULL) {
printf ("Error opening file: %s\n", strerror(errno));
exit(1);
}
for (i = 0; i < 8; i++)
{
fprintf(fp, "%d", parent[i]);
fprintf(fp, " ");
fprintf(fp, "%d", child[i]);
fprintf(fp, "\n");
}
for (i = 0; i < 8; i++)
{
fgets(current, 7, fp);
printf("%s\n", current);
curr2 = strtok(current, &space);
if (isFirst(parent, i) == 1)
{
parents[numParents].value = curr2;
parents[numParents].numChildren = 0;
numParents++;
}
printf("%s\n", curr2);
curr3 = strtok(NULL, &space);
while (done == 0)
{
temp = parents[j];
index = j;
if (temp.value == curr2)
{
done = 1;
}
j++;
}
j = 0;
printf("\n");
addChild(parents, numParents-1, curr3);
parents[index].numChildren++;
printf("%s\n", curr3);
done = 0;
}
fp2 = fopen ("/home/sam/parent2.txt","w+");
if (fp2 == NULL) {
printf ("Error opening file: %s\n", strerror(errno));
exit(1);
}
Child temp4;
for (i = 0; i < 3; i++)
{
fprintf(fp2, "%s", parents[i].value);
temp4 = *parents[i].next;
for (j = 0; j < parents[i].numChildren; j++)
{
fprintf(fp2, "%s", temp4.value); // segmentation fault occuring on this line second time through inner loop
fprintf(fp2, " ");
if (j < parents[i].numChildren-1)
{
temp4 = *temp4.next;
}
}
fprintf(fp2, "\n");
}
rewind(fp2);
for (i = 0; i < 3; i++)
{
fgets(current, 100, fp2);
printf("%s\n", current);
}
fclose(fp);
fclose(fp2);
return 0;
}
我想你的问题已经从这里开始了,在addChild函数中 将局部变量的地址分配给父结构的成员。当函数返回时,这将指向一个对象,该对象已从悬空指针中消失 以后任何使用该指针的行为都会引起各种各样的麻烦
在调试器下运行它!在linux终端上使用gdb后,我得到消息:程序收到信号SIGSEGV,分段错误。0x00007FF7AA5B91英寸??来自/lib/x86_64-linux-gnu/libc.so.6。您知道这意味着什么吗?很可能这意味着您没有使用-g编译,并且不知道如何使用where命令:我确实使用了-g,但我真的不知道如何使用gdb,所以不,我不知道如何使用where命令。好的,我刚开始编辑了我的简介,我只需要解释一下某两行代码有什么问题谢谢你的帮助。解决方案是将指针作为参数传递给父级吗?可能不是。我不太确定您想要做什么。获取比函数寿命长的对象的通常方法是使用malloc为它们分配空间。目标是为父数组中的每个数字都有一个链表。由于父数组中的索引0-2和7等于1,父对象将指向子对象[0],子对象将指向子对象[1]、子对象[2]和子对象[7]。然后我尝试使用指针将其写入文件。但是,再次感谢你的支持help@Sam您正在动态地构建一个数据结构,因此malloc无疑是一个不错的选择。
int addChild(Parent parents[], int index, char *num)
{
int i;
Child temp, temp2;
temp.value = num;
if (parents[index].numChildren == 0)
{
parents[index].next = &temp; // <---
temp.prev1 = &parents[index];
temp.prev2 = NULL;
}