为什么我在重新分配时不必从堆中释放内存?

Why dont i have to free memory from the heap when reallocating?

所以我有这个代码:

/* Dynamic Array Reader */

/* Parameters:
 * n: Number of values to be read
 * 
 * Returns: pointer to the dynamically allocated array
 */
int *dyn_reader(unsigned int n) {
    int* array = malloc(n * sizeof (int));
    if (!array)
        return NULL;
    else {
        unsigned int num_read = 0;
        printf("Enter %u integers so they can be put into this array\n", n);
        while (num_read < n) {
            num_read += scanf("%d", array + num_read);
        }
    }
    return array;
}

/* Add to array */

/* Parameters:
 * arr: Existing array of integers
 * num: number of integers in the array before the call
 * newval: new value to be added
 * 
 * Returns: pointer to the allocated array
 */
int *add_to_array(int *arr, unsigned int num, int newval) {
    int* newarray = realloc(arr, (num+1) * sizeof (int)); //allocate one more space
    if (newarray == NULL) //Return original array if failed to allocate
        return arr;

    //free(arr); //free old array -- this throws an error when i try and free up the old array
    newarray[num] = newval;
    return newarray;
}

int main()
{
    /* testing exercise. Feel free to modify */
    int *array = dyn_reader(5);

    array = add_to_array(array, 5, 10);
    array = add_to_array(array, 6, 100);
    array = add_to_array(array, 6, 1000);

    return 0;
}

如您所见,主函数调用 dyn_reader 分配足够的内存以允许数组中有 n 个元素。它从用户和 returns 数组中读取整数。

然后主函数调用add_to_array,它重新分配足够的内存以在数组中添加一个附加元素。如果不能,它 returns 原始数组。如果内存重新分配有效,我将 newval 添加到数组的末尾。在这种情况下,我使用一个新指针来存储新重新分配的数组的位置。为什么当我尝试释放旧数组 (free(arr);) 时,出现错误。该指针是否仍指向堆上的内存,我不应该释放它吗?

不,如果 realloc 移动到新的内存区域,那么它会为您执行 "free()"(因此请确保您没有任何其他指针指向该数组!)。 C标准说(在http://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html):

The realloc() function shall deallocate the old object pointed to by ptr

linux 手册页(位于 https://linux.die.net/man/3/realloc)使其更加明确:

 If the area pointed to was moved, a free(ptr) is done.

如果重新分配成功,realloc() 已处理释放与先前指针相关的内存。请注意,指针 可能甚至没有改变。

add_to_array() 的另一个问题是调用函数缺少 success/failure 的任何指示。