Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is there a mismatch in the value inside the function and outside the function?

I’m trying to store an array of pointers to a node. In the createlist() function, I have initialized the array of pointers with
linked_list_array[i] = createNode(-1); for each item in the array.

Then I assigned it to list->linkedLists with list->linkedLists = linked_list_array.
It seems that once I return list back into the main function, the value of list->linkedLists[0]->key changed.
Why is it that the value changed?

#include <stdio.h>
#include <stdlib.h>

#define NEG_INF (-32727)

struct leapList{
    
    int maxHeight;
    int curr_height;
    struct Node **linkedLists;
    double prob;
};

struct Node{
    struct Node* next;
    struct Node* below;
    int key;
};

struct Node* createNode(int key){
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
    new_node->next = NULL;
    new_node->below = NULL;
    new_node->key = key;
    return new_node;
}


struct leapList* createList(int maxheight, double p){
    struct leapList* list = (struct leapList*)malloc(sizeof(struct leapList));
    list->maxHeight = maxheight;
    list->prob = p;
    list->curr_height = 0;
    struct Node* linked_list_array[maxheight];
    for(int i=0;i<maxheight;i++){
        linked_list_array[i] = createNode(-1);
    }
    list->linkedLists = (linked_list_array);
    printf("key inside function: %d\n", list->linkedLists[0]->key);
    return list;
}
struct Node* insertNode(struct Node** headref, int key){
    struct Node* curr_node = NULL;
    struct Node* new_node = createNode(key);
    if(*headref == NULL || (*headref)->key >= key){
        new_node->next = *headref;
        *headref = new_node;
    }else{
        curr_node = *headref;
        while(curr_node->next!=NULL && curr_node->next->key < key){
            curr_node = curr_node->next;
        }
        new_node->next = curr_node->next;
        curr_node->next = new_node;
    }
    //printf("%d ", new_node->key);
    return new_node;
}


int main(int argc, const char * argv[]) {
    // insert code here...
    struct leapList* list = createList(5, 0.5);
    printf("key outside function: %d", list->linkedLists[0]->key);
    struct Node* head_node = createNode(-37273);

    
    insertNode(&head_node, 5);
    insertNode(&head_node, 3);
    insertNode(&head_node, 1);
    
    
    return 0;
}

This is what I get after running the program

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is what I get after running the program

>Solution :

You set list->linkedLists to point to a local ("automatic storage") variable.

...
struct Node* linked_list_array[maxheight];
...
list->linkedLists = linked_list_array;
...

It’s no longer legal to access linked_list_array after the function returns, yet that’s what you end up doing. This is Undefined Behaviour.

You will need to malloc the array.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading