1

I'm not sure if I'm missing something and that might make this question very stupid. But I can't understand why the hell it's failing after trying pretty much every approach one could have.

So, super simple, I have this GLib Tree and I want to insert stuff into it in other functions. Why none of the options presented below work? I can understand the first failing more than the second to be completely honest.

int compare_ints(gconstpointer gpa, gconstpointer gpb){
     int a = *((int*) gpa);
     int b = *((int*) gpb);
     return (a-b);
 }

void test1(GTree* tree){
     int code = 1234;
     gpointer gcp = &code;
     g_tree_insert(tree, gcp, gcp);
     printf("%d\n", (g_tree_lookup(tree, gcp) == NULL)); // Outputs 0 (obviously)
 }

 void test2(GTree** tree){
     int code = 1234;
     gpointer gcp = &code;
     g_tree_insert(*tree, gcp, gcp);
     printf("%d\n", (g_tree_lookup(*tree, gcp) == NULL)); // Outputs 0 (obviously)
 }

 int main(int argc, char** argv){
     GTree* tree = g_tree_new(compare_ints);
     int code = 1234;
     gpointer gcp = &code;
     test1(tree);
     printf("%d\n", (g_tree_lookup(tree, gcp) == NULL)); // Outputs 1 (Why?)
     test2(&tree);
     printf("%d\n", (g_tree_lookup(tree, gcp) == NULL)); // Outputs 1 (Why?)

     return 0;
 }

Sorry about it if it's a stupid question, any help at all apreciated :)

Edit: Removed vim line notation

pCosta99
  • 51
  • 5
  • `gpointer gcp = &code;` creates a pointer to a value with automatic storage duration. Inserting that pointer into the tree turns that pointer into a dangling pointer after the function exits – UnholySheep Mar 01 '20 at 19:02
  • Does this answer your question? [Can a local variable's memory be accessed outside its scope?](https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – UnholySheep Mar 01 '20 at 19:05
  • @UnholySheep to be honest I did not understand completely. What you mean is that I am indeed changing the tree but the change I made got "changed" when I left the function? If so, then how can I approach solving this problem? Because I need a gpointer and I need to insert things into the tree in other functions :/ – pCosta99 Mar 01 '20 at 21:09
  • @UnholySheep I looked it up and found out that I don't need to convert the int into a gpointer, I was confused by that fact in particular I guess. So, changing that should make the program work? (just asking because I don't have imediate acess to a place to test and one could answer) Thanks for the help, I'll mark this post as solved by your link once I manage to test that difference out! – pCosta99 Mar 01 '20 at 21:37
  • 1
    What you need to do is allocate the keys and values in a way that they stay "alive" longer than the function calls. e.g.: by using `malloc` (but then you need to make sure it gets `free`'d) – UnholySheep Mar 01 '20 at 21:42
  • Once again, thx a lot for the help! Worked like a charm ^^ @UnholySheep I'll cite your answer as the solution to this problem! – pCosta99 Mar 01 '20 at 22:29

1 Answers1

0

As @UnholySheep mentioned in the comments of the main thread, doing the following would work:

void test1(GTree* tree, int* code){
    g_tree_insert(tree, code, code);
    printf("%d\n", (g_tree_lookup(tree, code) == NULL));
}

void test2(GTree** tree, int* code){
    g_tree_insert(*tree, code, code);
    printf("%d\n", (g_tree_lookup(*tree, code) == NULL));
}

int main(int argc, char** argv){
    Catalog* c = init_catalog(26, 1, compare_ints);
    int* code = malloc(sizeof(int));
    *code = 1234;

    GTree* tree = g_tree_new(compare_ints);
    test1(tree, code);
    printf("%d\n", (g_tree_lookup(tree, code) == NULL));
    test2(&tree, code);
    printf("%d\n", (g_tree_lookup(tree, code) == NULL));

    destroy_catalog(c);
    free(code);
    return 0;
}

The reason this works is because code only dissapears when you free it!

On the initial case in the end of the function the int would stop existing and that would justify the behavior. If your interested in reading more about it check out the link that UnholySheep mentioned in the comments of the main thread!

pCosta99
  • 51
  • 5