I am trying to dynamically allocate memory for a algorithm on a graph. This is my code broken down to the problem:
typedef struct
{
int id;
int color;
} node_t;
unsigned int max_n = 2;
node_t* node_malloc = malloc(sizeof(node_t) * max_n);
node_t* nodes = node_malloc;
if(atoi(left_node) > max_n)
{
max_n = (size_t) atoi(left_node);
nodes = realloc(nodes, sizeof(node_t) * max_n);
}
For max_n < 9 the realloc works - when max_n is 9 or greater, "free(): invalid pointer:" is thrown.
free(node_malloc);
When free(node_malloc) is called after a realloc happened it throws "free(): invalid next size (fast)"
Thank you for your answers!
atoi()and all this. and put to account that you dont care to packstruct's so there're many gaps and it will definitely turn out bad when something custom is done on that.