1

在写入由变量 fp2 表示的文件 parent2.txt 时出现分段错误。我在程序停止运行的行和负责该问题的行上添加了注释。我猜测分段错误是“temp4.value”设置为空的结果,即使我认为我给了它一个值。“temp2 = *temp2.​​next”这行似乎有问题。temp2 是子结构的变量。我不知道代码到底有什么问题,这就是我需要帮助的地方。这是我的代码:

#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;
}
4

1 回答 1

0

我认为您的问题已经从这里开始,在addChild函数中

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;
    }

您将局部变量的地址分配给Parent结构的成员。当函数返回时,this 将指向一个已经消失的对象(一个悬空指针)。

以后对该指针的任何使用都会引起各种麻烦。

   

于 2012-06-23T02:25:29.460 回答