J N
J N

Reputation: 23

Thread 1: EXC_BAD_ACCESS (code=1, address=0x800000012)

I've tried to look up the solution to this problem through various other threads but my search was unsuccessful. I'm new to C and also new to this site so I apologize in advance if I'm incorrect in phrasing this question. I kinda have an idea of what's going on, but at the same time I might be entirely wrong. I have a linked list and I'm trying to insert at the end of the list. But when Xcode gets to the statement while(ptr->next!=NULL) it throws the error:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x800000012)

I read somewhere before that it was because I'm accessing something that doesn't exist or I'm failing to initialize node->next to NULL but I did in the previous "if statement". I'm pretty new at coding with pointers and linked lists and again I apologize for any weird stuff that might be in my code ):

////LIST AND NODE STRUCTURES

//data nodes
typedef struct node{
    int data;
    int ID;
    struct node* prev;
    struct node* next;
} node;


typedef struct ListInfo{
    int count; //numnodes
    struct node *list; //list of nodes
} ListInfo;

////INSERT FUNCITON

 void insert(ListInfo *H, node *n){
        if(n == NULL)
            return;

        node* ptr = H->list;
        if(H==NULL){
            ptr = n;
            ptr->next = NULL;
        }
        else{
            while(ptr->next!=NULL){ //Thread 1: EXC_BAD_ACCESS (code=1, address=0x800000012)
                ptr = ptr->next;
            }
            ptr = n;
            ptr->next = NULL;
        }
        // End of function
        return;
    } 

////MAIN

int main(){ // No Edititng is needed for the main function.

    ListInfo H;
    H.count =0;

    node *n;
    int Data = 0,ID =0 ;

    do{
        printf("Enter an ID and a Value to add to the list, Enter -1 to stop: ");
        //Get value from user to store in the new linked list node later.
        scanf("%d %d",&ID,&Data);

        // Check if the user entered "-1", if so exit the loop.
        if(Data == -1||ID == -1)
            return 0;

        // Allocate memory for a new Node to be added to the Linked List.
        n = malloc(sizeof(node));

        // Put the Data from the user into the linked list node.
        n->data = Data;
        n->ID = ID;

        //Increment the number of nodes in the list Header.
        // If the current node count is zero, this means that this node is the first node
        //   in this list.
        if(H.count++ == 0)
            H.list = n;
        // Otherwise, just use the insert function to add node to the list.
        else insert(&H,n);

    }while(Data != -1);

    // Display all nodes in the list.
    DisplayList(&H);


    //Remove a node from the list, and display the list each time.
    while(H.count != 0){
        Delete(&H,H.list->data);
        DisplayList(&H);
    }

    // Display the list, this should be empty if everything was correct.
    DisplayList(&H);
}

Upvotes: 1

Views: 119

Answers (1)

jmq
jmq

Reputation: 1591

When you allocate n you never set n->next. When you pass it to insert() you try to access the bad pointer and crash. When you set n->ID you should set n->next to NULL.

Upvotes: 1

Related Questions