Question

I'm trying to write a simple program that gets a list from the user (a list is a struct with data, and a pointer to the next list), and then print it. My code is working fine, but after printing, I get an error "Unhandled exception at 0x011e1502 in exercise 4.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd."

Can anyone tell me why? here's my code:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef int list_data;
typedef struct list
{
    list_data number;
    struct list* next_list;
} list; //definition of a list
list* create_list()
{
    list* anchor;
    anchor=(list*)malloc(sizeof(list));
    anchor->next_list=NULL;
    return anchor; // allocates a memory for a list and returns address of first block
}
list* insert_list(list* current_position,list_data x)
{
    list* temp;
    temp=(list*)malloc(sizeof(list));
    temp->number=x;
    temp->next_list=current_position->next_list;
    current_position->next_list=temp;
    return temp; //inserts a new block with a data of x 
}
void printlist(list* anchor)
{
    list* current_list=anchor->next_list;
    while(current_list!=NULL)
    {
        printf("%3d -> ",current_list->number);
        current_list=current_list->next_list;
    }
    printf("End\n");
}
void scan_list(list* anchor)
{
    int num1=1;
    list* current_position=anchor;
    printf("Enter values until 0\n");
    while(num1!=0)
    {
        scanf("%d",&num1);
        if(num1)
            current_position=insert_list(current_position,num1);
    }
}
void main()
{
    list* anchor;
    anchor=create_list();
    scan_list(anchor);
    printf("\n");
    printlist(anchor);
    free(anchor);
    getch();
}
Was it helpful?

Solution

You are accessing an unitialized memory-area, which is denoted by the value 0xcdcdcdcd. You must not simply free your list by deleting the first element, but by deleting all elements, iterating through your list and freeing every node, because you would create a memory-leak otherwise

void free_list(list* anchor){
    list* temp = anchor->next_list;
    free(anchor);
    while(temp->next_list){
       list* l = temp->next_list;
       free(temp);
       temp = l->next_list;
    }
}

Additionally, set the next element explicitly to NULL, when adding a node and you are having a weird cross-reference in your function

list* insert_list(list* current_position,list_data x)
{
    list* temp;
    temp=(list*)malloc(sizeof(list));
    temp->number=x;
    //temp->next_list=current_position->next_list; -> doesn't make sense
    current_position->next_list=temp;
    temp->next_list = NULL; //set to NULL
    return temp; //inserts a new block with a data of x 
}

I think four you have not explicitly told that the next item is NULL you are iterating past the actual end of the list.

OTHER TIPS

The code you posted is working fine. It is taking values from user and displaying it correctly. I have compiled the code using linux gcc and made some modification to avoid some warning

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top