0

I'm printing out the Head and Tail of the Linked List when an element is added, Fairly simple.

int main(){

    struct node{
        struct node* next;
        struct node* previous;
        double value;
    };

    struct LinkedList{
        struct node* head;
        struct node* tail;
    };

    void addValue(struct LinkedList* list,double newValue){
        struct node newNode;
        newNode.next = NULL;
        newNode.value=newValue;
        if(list->head == NULL){
            newNode.previous=NULL;
            list->head= &newNode;
            list->tail=&newNode;


        }
        else
        {
            newNode.previous= list->tail;
            list->tail->next= &newNode;
            list->tail= &newNode;



        }
            printf("%f\n",list->head->value);
            printf("%f\n",list->tail->value);

    }

    struct LinkedList l1;
    l1.head=NULL;
    l1.tail=NULL;
    addValue(&l1,5);
    addValue(&l1,6);
    addValue(&l1,7);
    addValue(&l1,8);


}

But the output I get is

5.000000 5.000000 6.000000 6.000000 7.000000 7.000000 8.000000 8.000000

Instead what I expect

5.000000 5.000000 5.000000 6.000000 5.000000 7.000000 5.000000 8.000000

Any idea why?

4
  • 2
    you should create new nodes on the heap with the malloc command instead of on the stack as currently is done. Commented Jan 28, 2014 at 19:22
  • @catchmeifyoutry Good catch, it looks like dangling pointers. Commented Jan 28, 2014 at 19:23
  • @remyabel you can try to write my catch in an answer if you want, I have some other stuff to do ;) Commented Jan 28, 2014 at 19:25
  • Guess I was sleepy :P Commented Jan 28, 2014 at 19:35

2 Answers 2

4

As stated in the comments, you should create newNode on the heap. When the function exits, the pointers are no longer pointing to valid memory. Luckily, the changes you need to make are minimal.

    struct node* newNode = (struct node*)malloc(sizeof(struct node));
    newNode->next = NULL;
    newNode->value=newValue;
    if(list->head == NULL){
        newNode->previous=NULL;
        list->head= newNode;
        list->tail=newNode;
    }
    else
    {
        newNode->previous= list->tail;
        list->tail->next= newNode;
        list->tail=newNode;
    }

And to avoid memory leaks you should free your pointers. Here's an example implementation:

void deleteList(struct node** head_ref)
{
   struct node* current = *head_ref;
   struct node* next;

   while (current != NULL)
   {
       next = current->next;
       free(current);
       current = next;
   }

   *head_ref = NULL;
}
deleteList(&l1.head);

I tested it in valgrind and that should eliminate your leaks.

Sign up to request clarification or add additional context in comments.

2 Comments

And don't forget to implenent delete function so the memory will be released at the end
maybe you can mention that the memory should be freed too, to complete the implementation
0

By defining struct node newNode; in void addValue(struct LinkedList* list,double newValue), memory gets allocated on the stack. so when control from the function returns, no more access to stack. values are gone !!!!. Thats its required to allocate from heap, dynamic memory where it cna be accesses until it is freed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.