I have a function that makes a matrix bigger in C. it works the first time you use it, but on the second time, I get "realloc(): invalid old size". Any ideas on what might be happening?
Here's the code:
int** resize_matrix(int** matrix, long org_size, long size_inc){
long new_size = org_size + size_inc; int** new; int* temp; new = realloc(matrix, new_size*sizeof(int*)); for(long i=0; i<org_size; i++){ //error happens here, when i=0, but only the second time you run it temp = realloc(new[i], new_size*sizeof(int)); new[i] = temp; //clears the added space for(long i2=org_size; i2<new_size; i2++){ new[i][i2] = 0; } } for(long i = org_size; i<new_size; i++){ new[i] = calloc(new_size, sizeof(int)); }
return new;
}
I call the function with:
adjacency_matrx = resize_matrix(adjacency_matrix, number_of_nodes, 100);