释放分配内存:realloc的()与免费() [英] Freeing allocated memory: realloc() vs. free()

查看:140
本文介绍了释放分配内存:realloc的()与免费()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一张与分配的内存的malloc()后来与改变的realloc()

so I have a piece of memory allocated with malloc() and changed later with realloc().

在我的code某些时候,我想清空,我的意思基本上是给它的东西0。这将直观地用做的realloc的内存(指针,0)。我在这里读,这是实现定义,不应使用。

At some point in my code I want to empty it, by this I mean essentially give it memory of 0. Something which would intuitively be done with realloc(pointer,0). I have read on here that this is implementation defined and should not be used.

我应该改用免费(),然后再做另一个的malloc()

Should I instead use free(), and then do another malloc()?

推荐答案

这取决于你的意思:如果你想的清空使用的内存的,但仍然可以访问内存,那么你使用的memset(指针,0,mem_size); ,重新初始化该内存以零结果。
如果你不再需要内存,那么你只需拨打免费(指针); ,which'll释放内存,因此它可以用在别处。

It depends on what you mean: if you want to empty the memory used, but still have access to that memory, then you use memset(pointer, 0, mem_size);, to re-initialize the said memory to zeroes.
If you no longer need that memory, then you simply call free(pointer);, which'll free the memory, so it can be used elsewhere.

使用的realloc(指针,0)可工作就像免费您的系统,但这标准行为。 的realloc(PTR,0)不是由C99或C11标准规定为相当于免费(PTR)

Using realloc(pointer, 0) may work like free on your system, but this is not standard behaviour. realloc(ptr, 0) is not specified by the C99 or C11 standards to be the equivalent of free(ptr).

标准(C99,§7.22.3.5):

The standard (C99, §7.22.3.5):


The realloc function
Synopsis
1
#include <stdlib.h>
void *realloc(void *ptr, size_t size);

Description
2 The realloc function deallocates the old object pointed to by ptr and returns a
pointer to a new object that has the size specified by size. The contents of the new
object shall be the same as that of the old object prior to deallocation, up to the lesser of
the new and old sizes. Any bytes in the new object beyond the size of the old object have
indeterminate values.
3 If ptr is a null pointer, the realloc function behaves like the malloc function for the
specified size. Otherwise, if ptr does not match a pointer earlier returned by a memory
management function, or if the space has been deallocated by a call to the free or
realloc function, the behavior is undefined. If memory for the new object cannot be
allocated, the old object is not deallocated and its value is unchanged.
Returns
4
The realloc function returns a pointer to the new object (which may have the same
value as a pointer to the old object), or a null pointer if the new object could not be
allocated.

正如你所看到,这里的大小为0。相反,它只是说一个NULL指针返回失败分配内存,并在所有其他情况下,指针不指定realloc的调用一个特例。指向0字节的指针会,然后,是一个可行的选择。

As you can see, it doesn't specify a special case for realloc calls where the size is 0. Instead, it only states that a NULL pointer is returned on failure to allocate memory, and a pointer in all other cases. A pointer that points to 0 bytes would, then, be a viable option.

引用<一个href=\"http://stackoverflow.com/questions/16759849/using-realloc-x-0-instead-of-free-and-using-malloc-with-length-of-a-string\">a相关的问题:

更直观,realloc的是在概念上等价于对malloc +的memcpy +免在另一指针,和的malloc-ING存储器的0字节块返回是NULL任一个唯一指针,不被用于存储任何(你要的0字节),但仍有待freeed。所以,不,不要使用realloc的那样,它可能会在某些实现(即Linux)的工作,但它肯定不能保证。

More intuitively, realloc is "conceptually equivalent" to to malloc+memcpy+free on the other pointer, and malloc-ing a 0-byte chunk of memory returns either NULL either a unique pointer, not to be used for storing anything (you asked for 0 bytes), but still to be freeed. So, no, don't use realloc like that, it may work on some implementations (namely, Linux) but it's certainly not guaranteed.

由于在该链接的问题状态另一个答案,的realloc的行为(PTR,0)被明确定义为的实现定义的根据目前C11标准:

As another answer on that linked question states, the behaviour of realloc(ptr, 0) is explicitly defined as implementation defined according to the current C11 standard:

如果该空间的请求的大小是零,该行为是实现定义:无论是空指针被返回,或者行为好像大小是一些非零值,不同的是返回指针不应被用来访问对象

If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object

这篇关于释放分配内存:realloc的()与免费()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆