以下のコードの最後で、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 でサイズを変更する必要がありますか? malloc)?