在下面代码的末尾,我需要将哪个指针插入 free()、array 或 temp_array?哪一个或释放内存块是否重要?
int *array = 0;
int *temp_array = 0;
int count = 0;
array = malloc(sizeof(int));
// skipping code where count is calculated...
temp_array = realloc(array, count * sizeof(int));
if (temp_array == NULL) {
free(array);
// some error message
return;
}
array = temp_array;
// skipping section of code, which reads numbers from a file and loads them into an array
// amount of numbers read can be 0 to whatever
free (array); // free array or temp_array?
另外,如果尝试为其分配内存的指针为NULL,是否可以使用realloc分配内存块(换句话说,我是否需要先用malloc分配内存,然后再用realloc调整它的大小,或者我可以跳过马尔洛克)?