1

Im not sure i got the concept of Linked List properly. What im trying to do is to create a linked list which receives the integer 6 as it's first "data". instead i get this access violation error when trying to write in the integer into the first node's data. is there something specific i missed here?

    ///////////////////////////////

typedef struct List_Node
{
    int data;
    struct List_Node* next;
}List_Node;

typedef struct List
{
    List_Node* head;
}List;

////////////////////////////////


List* create_list();
void print_list(List_Node *x);
void add_to_node(List_Node *a,int val);

////////////////////////////////

void main()
{
    List *a = create_list();
    List_Node *ind = a->head;
    printf("List:\n");
    add_to_node(ind,6);
}

void print_list(List_Node *a)
{
    while(a != NULL)
    {
        printf("%d \n",a->data);
        a = a->next;
    }
    return;
}

void add_to_node(List_Node *a,int val)
{
    a->data = val;
}

struct List* create_list()
{
struct List* list = (List*) malloc(sizeof(List));
list->head = NULL;
return list;
}
Anish Ramaswamy
  • 2,326
  • 3
  • 32
  • 63
Michael Kross
  • 47
  • 1
  • 4
  • 2
    "Im not sure i got the concept of Linked List properly.", I think what you did not get properly, is *concept of pointer*. But good thing is, implementing a linked list is a good way to learn pointers. – hyde Apr 02 '13 at 08:52
  • 1
    usually we suggest beginners to use a debugger, but in this case it's so simple you can follow the code with paper and pencil... – Karoly Horvath Apr 02 '13 at 08:58

3 Answers3

2

The code is dereferencing a NULL pointer as a->head is NULL:

list->head = NULL; /* inside create_list()` and 'list' returned to ...*/

List_Node *ind = a->head; /* ... inside main(), and then passed to */
add_to_node(ind,6);       /* add_to_node() ... */

a->data = val;            /* and dereferenced inside add_to_node(). */

Dereferencing a NULL pointer is undefined behaviour. To correct, malloc() memory for a List_Node and assign to a->head. Recommend creating an add_node() function that allocates memory and assigns the new int value to newly malloc()d node.


Do I cast the result of malloc?

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
1

As all have pointed out, you are dereferencing a NULL pointer as your list->head contains NULL.

Another thing I should point out is that, you are creating a List. but not any Node. There is no node in the list. You have to allocate memory for a Node and then use it.

So, instead of add_to_node(), you may use a function add_node that will take the list or the head and the value as parameters, create a node(i.e. allocating memory for the node), set the value and add it to the list.

Also, in your case, the structure List is redundant as it contains only one member. instead you can simply use List_node* head.

Dipto
  • 2,720
  • 2
  • 22
  • 42
1

What you are doing:

In create_list:

  • Allocating memory for a List pointer.
  • Setting the list's head to NULL.

In add_to_node:

  • Setting the specified node pointer's data element to the specified val.

In main:

  • Creating a List pointer a by calling create_list. This list has a NULL head.
  • Initializing a List_Node pointer, ind, to point to the created list's head (which is NULL).
  • Trying to set ind's data element to 6 by calling add_to_node.
    • This is where your program is causing the access violation exception.
    • ind = NULL. Therefore NULL->data = undefined behaviour.

What you should be doing:

In create_list:

  • Allocate memory for a List pointer, say linked_list.
  • Allocate memory for linked_list's head pointer.
  • For the linked_list's head, initialize data and the next pointer to 0 and NULL respectively.

In add_to_node:

  • Do the same thing you're doing now.

In main:

  • Create a List pointer a by calling create_list. Now, this list will have a valid, initialized NULL head, but with no meaningful data.
  • Set the list's head data by calling add_to_node(a->head, 6);.

Note: This will only ensure you have a head node in your list. Nowhere are you creating additional nodes.

Community
  • 1
  • 1
Anish Ramaswamy
  • 2,326
  • 3
  • 32
  • 63