简体   繁体   中英

Cannot access memory at address at address “” (gdb)

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

I'm relearning some of this but I'm not sure where I'm going wrong.

I get a segmentation fault when the program reaches the Traverse() function.

I can access the Node that before the iteration of the while loop is not complete. As soon as the next iteration begins the myList -> start -> next can't be accessed any longer.

In Create node method:

malloc (sizeof(struct Node *))

Will allocate only 4 or 8 bytes because it sizeof pointer.

It should be:

malloc (sizeof( Node ))

to allocate space for an object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM