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/
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;