Please hold in mind that malloc, calloc and realloc can fail and return NULL.
Therefor you should allways check the returned value of malloc, calloc and realloc.
In case of realloc the old pointer is still valid.
Code:
…
ND[i].ic = realloc(ND[i].ic, k*sizeof(int));
…
If realloc fails here, ND[i].ic will point to NULL, and the old pointer is valid, but where is the old pointer?
Without the old pointer you can't free the memory later.
Code:
void alloc_test(NODE_DATA *ND, int c) {
int m = 10;
int i, j, k;
int *temp_int;
double *temp_double;
if (ND == NULL) return; // we don't work with NULL pointer
for (i = 0; i < c; i++) {
if ((ND[i].ic = malloc(sizeof(int))) == NULL) {
fprintf(stderr, "No memory left for ND[%d].ic\n", i);
return;
}
if ((ND[i].wc = malloc(sizeof(double))) == NULL) {
fprintf(stderr, "No memory left for ND[%d].wc\n", i);
return;
}
k = 0;
for (j = 0; j < m; j++) {
k++;
if ((temp_int = realloc(ND[i].ic, k*sizeof(int))) == NULL) {
fprintf(stderr, "No memory left for ND[%d].ic in size of %d !\n", i, k);
return;
}
else {
ND[i].ic = temp_int;
}
if ((temp_double = realloc(ND[i].wc, k*sizeof(double))) == NULL) {
fprintf(stderr, "No memory left for ND[%d].wc in size of %d !\n", i, k);
return;
}
else {
ND[i].wc = temp_double;
}
}
}
}
Now, the function has return points in case of fails.
You should thing of another return value of the function so that the caller become a feedback.