-2

I'm fairly new to c and I'm implementing a DHT. For that, I have to store node IDs in a list.

Node IDs are < 2^160 whole numbers(thus resulting in me using GMP), but I'm setting 123 as an ID here to keep the example clean.

I am able to set the Node's ID just fine, and I also verify it with mpz_out_str. When I iterate over it with g_slist_foreach and print the ID, it outputs a HUGE number, which is completely irrelevant.

I suspect it has something to do with the cast from gpointer to Node, or that I may be storing it incorrectly(very new to pointers)
Code:

#include <glib.h>
#include <gmp.h>
#include <stdio.h>

typedef struct {
  mpz_t *node_id;
} Node;

void print_iterator(gpointer item, gpointer prefix) {
  // Also, since the item is of type gpointer,
  // It has to be casted back to Node.
  Node *node = (Node *)item;
  // outputs a HUGE, completely random number.
  mpz_out_str(stdout, 10, node->node_id);
  printf("\n");
};

GSList *add_node(GSList *nodes) {
  Node *node = malloc(sizeof(Node));
  mpz_t id;
  mpz_init(id);
  char *f = "123";
  mpz_set_str(id, f, 10);
  node->node_id = &id;
  // prints correct value
  mpz_out_str(stdout, 10, node->node_id);
  nodes = g_slist_append(nodes, node);
  return nodes;
}

int main(int argc, char const *argv[]) {
  GSList *nodes = NULL;
  nodes = add_node(nodes);
  g_slist_foreach(nodes, print_iterator, "‑‑>");
  return 0;
}

Relevant links:

https://developer.gnome.org/glib/stable/glib-Singly-Linked-Lists.html
https://gmplib.org/manual/

Aarnav
  • 3
  • 1
  • Don't use both C and C++ tags. If you used a C++ compiler, you should get an error telling you that you are using wrong pointer types. – Marc Glisse Jan 02 '21 at 17:11

1 Answers1

1

With node->node_id = &id; your node_id points at a local variable. When add_node returns, id is destroyed and your pointer is dangling. Dereferencing it results in Undefined Behavior.

A simple solution is to store the id in Node rather than storing a pointer to it.

typedef struct {
  mpz_t node_id;
} Node;
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • Cheers for the quick response. However, I would like to store it in a pointer as most GMP functions require pointers, and it would be slightly more annoying to handle that if I store it without one. – Aarnav Jan 02 '21 at 17:02
  • 1
    @Aarnav Then you will have to allocate memory to store the node id and free it when you're done. – 1201ProgramAlarm Jan 02 '21 at 17:14