Happy holidays C gurus! I am running into a strange error using realloc to resize a 2D array within a function and could use some advice.

The function is designed to remove duplicate entries from a dynamically allocated 2D array of integers with 3 columns. The function is used inside a for loop and gives the illusion of working for the first 4 repeats. On the fifth repeat, malloc returns a NULL value at the point indicated in the code below. The error occurs at exactly the same point every time I run the simulation on a LINUX machine. However, I get no error messages running the same code on a Windows machine.

I know it is not a particularly elegant function, but are there any obvious memory leaks or is it something to do with the machine itself?

Thanks in advance for your help.

Code:Code:int** condense_rewired(int **rewired_edges, long int num_rewired_edges) { /* QSORT LIST BY COL 3, COL 1, COL 2 */ int comp_rewired(const void* a, const void* b) { int **p1 = (int**)a; int **p2 = (int**)b; int *arr1 = *p1; int *arr2 = *p2; int diff3 = arr1[2] - arr2[2]; if (diff3) return diff3; int diff2 = arr1[0] - arr2[0]; if (diff2) return diff2; return arr1[1] - arr2[1]; } qsort(rewired_edges, num_rewired_edges, sizeof(rewired_edges[0]), comp_rewired); int **temp_edges = (int**)malloc( sizeof(int *) * 1); temp_edges[0] =(int*)malloc( sizeof(int) * 3); long int x = 0; long int num_temp_edges = 1; temp_edges[0][0] = rewired_edges[0][0]; temp_edges[0][1] = rewired_edges[0][1]; temp_edges[0][2] = rewired_edges[0][2]; for(x = 1; x < num_rewired_edges; x++) { if( rewired_edges[x][0] == rewired_edges[(x - 1)][0] && rewired_edges[x][1] == rewired_edges[(x - 1)][1] && rewired_edges[x][2] == rewired_edges[(x - 1)][2] ) { } else { temp_edges = (int **)realloc(temp_edges, sizeof(int*) * (num_temp_edges + 1)); temp_edges[num_temp_edges] = (int*)malloc(sizeof(int) * 3); if(temp_edges[num_temp_edges] == NULL) { printf(" temp_edges malloc fail \n"); } temp_edges[num_temp_edges][0] = rewired_edges[x][0]; temp_edges[num_temp_edges][1] = rewired_edges[x][1]; temp_edges[num_temp_edges][2] = rewired_edges[x][2]; num_temp_edges = num_temp_edges + 1; } } rewired_edges = (int **)realloc(rewired_edges, sizeof(int*) * (num_temp_edges)); for(x = 0; x < num_temp_edges; x++) { rewired_edges[x] = (int*)malloc(sizeof(int) * 3); if( rewired_edges[x] == NULL) { printf("malloc failed here, x = %ld \n", x); /* **** CRASHES HERE **** */ } rewired_edges[x][0] = temp_edges[x][0]; rewired_edges[x][1] = temp_edges[x][1]; rewired_edges[x][2] = temp_edges[x][2]; } num_rewired_edges = num_temp_edges; for(x = 0; x < num_temp_edges; x++) { free(temp_edges[x]); } free(temp_edges); return(rewired_edges); }