C语言 C - 如果使用 realloc 是否需要免费?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5426700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C - If realloc is used is free necessary?
提问by Cory
When using realloc is the memory automatically freed? Or is it necessary to use free with realloc? Which of the following is correct?
使用 realloc 时内存是否自动释放?或者是否有必要将 free 与 realloc 一起使用?以下哪个是正确的?
//Situation A
ptr1 = realloc(ptr1, 3 * sizeof(int));
//Situation B
ptr1 = realloc(ptr2, 3 * sizeof(int));
free(ptr1);
ptr1 = ptr2;
回答by Scorpions4ever
Neither is correct. realloc() can return a pointer to newly allocated memory or NULL on error. What you should do is check the return value:
两者都不正确。realloc() 可以返回一个指向新分配内存的指针,或者在出错时返回 NULL。你应该做的是检查返回值:
ptr1 = realloc(ptr2, 3 * sizeof(int));
if (!ptr1) {
/* Do something here to handle the failure */
/* ptr2 is still pointing to allocated memory, so you may need to free(ptr2) here */
}
/* Success! ptr1 is now pointing to allocated memory and ptr2 was deallocated already */
free(ptr1);
回答by Erik
After ptr1 = realloc(ptr2, 3 * sizeof(int));ptr2 is invalid and should not be used. You need to free ptr1only. In somecases the return value of reallocwill be the same value you passed in though.
ptr1 = realloc(ptr2, 3 * sizeof(int));ptr2后无效,不应使用。你ptr1只需要免费。在某些情况下, 的返回值realloc将与您传入的值相同。
You can safely consider ptr1=realloc(ptr2, ...as equivalent to this:
您可以放心地将其ptr1=realloc(ptr2, ...视为等效于:
ptr1 = malloc(...);
memcpy(ptr1, ptr2, ...);
free(ptr2);
This is what happens in most cases, unless the new size still fits in the old memory block - then realloc could return the original memory block.
大多数情况下都会发生这种情况,除非新大小仍然适合旧内存块 - 然后 realloc 可以返回原始内存块。
As other allocation functions, reallocreturns NULL if it fails - you may want to check for that.
与其他分配函数一样,realloc如果失败则返回 NULL - 您可能需要检查一下。
回答by geekosaur
realloc()automatically frees the original memory, or returns it unchanged (aside from metadata) if you realloc()to a smaller size or there is unallocated memory available to simply expand the original allocation in place.
realloc()自动释放原始内存,或将其原样返回(元数据除外),如果您realloc()使用较小的大小或有未分配的内存可用于简单地扩展原始分配到位。
回答by Adam Trhon
According to http://www.cplusplus.com/reference/clibrary/cstdlib/realloc/, reallocreturns the new memory block if the reallocation is successful, keeps the original memory block if not. Standard usage would look like this:
根据http://www.cplusplus.com/reference/clibrary/cstdlib/realloc/,realloc如果重新分配成功,则返回新的内存块,否则保留原始内存块。标准用法如下所示:
ptr2 = realloc(ptr1, new_size);
if (ptr2 == NULL) {
//ptr1 still contains pointer to original array
} else {
//ptr2 contains new array, ptr1 is invalid
}
回答by wallyk
In both situations, ptr1 must be freed.
在这两种情况下,都必须释放 ptr1。
Situation B is more complex because ptr2 potentially points to freed space. Or not. It depends on whether it could be reallocated. ptr2 should not be used after the realloc in B.
情况 B 更复杂,因为 ptr2 可能指向已释放的空间。或不。这取决于它是否可以重新分配。在 B 中的重新分配之后不应使用 ptr2。

