C - realloc() I Keep getting Segmentation fault (core dumped)
I have been getting generic Segmentation fault (core dumped) error for a day now. I just can't figure out what's the problem with my code.
Code:
typedef struct Node {
int id;
int type;
float value;
float bias;
float pos;
} Node;
// Global
int global_nodes_capacity = 64;
int n_global_nodes = 0;
Node *global_nodes;
void increase_nodes_capacity(Node *nodes, int *capacity, int needed) {
if (needed > *capacity) {
*capacity = pow(2, ceil(log2(needed)));
nodes = (Node *) realloc(nodes, *capacity * sizeof(Node));
}
}
// In main()
global_nodes = (Node *) malloc(global_nodes_capacity * sizeof(Node));
// In another function called from main()
increase_nodes_capacity(global_nodes, &global_nodes_capacity, n_global_nodes + 1);