【问题标题】:Cannot access memory at address at address "" (gdb)无法访问地址“”处的内存(gdb)
【发布时间】:2018-09-08 07:20:51
【问题描述】:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

struct Node {

    /* Data fields with appropraiate types */
    char fname[64];
    char lname[64];
    char puid[16];
    int Age;
    struct Node *next;
};

struct List {

    struct Node *start;

    int numNodes;
};

struct List * initialize_list() {

    struct List *list = (struct List *) malloc(sizeof(struct List *));

    list -> numNodes = 0;
    list -> start = (struct Node *) malloc(sizeof (struct Node *));

    return list;
}

struct List * CreateList() {

    struct List *list = initialize_list();

    return list;
}

void Traverse(struct List *list) {

    struct Node *node = (struct Node *) malloc (sizeof(struct Node));
    int i = 1;

    node = list -> start -> next;

    while (node != NULL) {

        printf ("\nNode: %d", i);
        printf("\nPUID: %s", node -> puid);

        node = node -> next;
        i++;
    }

    if (i == 1) {

        printf("\n\nEmpty List");
    }
}

struct Node *CreateNode(char first_name[], char last_name[], char PUID[], int age) {

    struct Node *newNode = (struct Node *) malloc (sizeof(struct Node *));

    strcpy(newNode->fname, first_name);
    strcpy(newNode->lname, last_name);
    strcpy(newNode->puid, PUID);
    newNode->Age = age;
    newNode->next = NULL;

    return newNode;
}

void InsertFront(struct List *list, char first_name[], char last_name[], char PUID[], int age) {

    struct Node *newNode = CreateNode (first_name, last_name, PUID, age);

    if (list -> numNodes == 0) {

        list -> start -> next = newNode;

        list -> numNodes++;

        return;
    }

    newNode -> next = list -> start -> next;
    list -> start -> next = newNode -> next;

    list -> numNodes++;

    return;
}

int main () {

    struct List *myList = CreateList();

    while (1) {

        int option = 0;
        char fname[64];
        char lname[64];
        char puid[16];
        int age;

        printf("\n0. Exit Program \n1. Insert Front\n2. Insert Middle\n3. Insert End\n4. Delete Front\n5. Delete Middle\n6. Delete End\n7. Traverse \n8. Look Up by Index\n");
        printf ("Enter option: ");
        option = getchar();

        if (option == '0') {

            exit (0);
        }
        else if (option == '1' || option == '2' || option == '3') {

            printf("Enter first name: ");
            scanf("%s", fname);

            printf("Enter last name: ");
            scanf("%s", lname);

            printf("Enter PUID: ");
            scanf("%s", puid);

            printf("Enter age: ");
            scanf("%d", &age);

            if (option == '1') {

                InsertFront (myList, fname, lname, puid, age);
            }
            else if (option == '2') {

                int index;

                printf ("Enter position to Insert: ");
                scanf ("%d", &index);

                InsertMiddle (myList, index, fname, lname, puid, age);
            }
            else if (option == '3') {

                InsertEnd (myList, fname, lname, puid, age);
            }
        }
        else if (option == 4) {

        }
        else if (option == 5) {

        }
        else if (option == 6) {

        }
        else if (option == '7') {

            Traverse (myList);
        }
        else if (option == 8) {

        }
        else {

        }
        getchar();
    }

    return 0;
}

我正在重新学习其中的一些内容,但我不确定我哪里出错了。

程序到达 Traverse() 函数时出现分段错误。

我可以访问在while循环的迭代未完成之前的节点。一旦下一次迭代开始, myList -> start -> next 就不能再访问了。

【问题讨论】:

  • 输入是什么?
  • 在初始化后list-&gt;startmalloc()ed 但未初始化,尤其是。 list-&gt;start-&gt;next 包含垃圾值
  • 你有内存泄漏。
  • foo = (bar *)malloc(..) 是 C 语言中的redurent,更合适的是foo = malloc(...)
  • 这真的是显示您的问题的最小代码示例吗?

标签: c memory struct linked-list gdb


【解决方案1】:

Create node 方法中:

malloc (sizeof(struct Node *))

将只分配 4 或 8 个字节,因为它是 sizeof 指针。

应该是:

malloc (sizeof( Node ))

为对象分配空间

【讨论】:

    猜你喜欢
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 2013-02-18
    • 1970-01-01
    相关资源
    最近更新 更多