malloc和realloc函数 [英] malloc and realloc functions

查看:130
本文介绍了malloc和realloc函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main( )    
{
    int *p;
    p=(int *)malloc(20);
    printf("%u",p); \\ printing some memory adress 
    p=(int *)realloc(p,0);
    printf("%u",p); \\ printing as 0
    printf("%u",*p); \\ printing the value as 0
}

现在我的问题是语句 relloc 工作作为免费()函数指针 p 指向其中的 NULL NULL 值。将有20个字节被释放?

Now my question is the statement relloc working as the free() function as the pointer p is pointing to the NULL and NULL value in it. Will that 20 bytes be freed?

推荐答案

没有,它可能不会在的所有的情况下释放。 C99有这样一段在说7.20.3.4的realloc函数

No, it may not be freed in all circumstances. C99 has this to say in 7.20.3.4 The realloc function:

如果新对象的内存无法分配,旧的对象不释放其价值是不变的。

If memory for the new object cannot be allocated, the old object is not deallocated and its value is unchanged.

现在你可能认为的零字节分配不可能失败,但标准并不强制。它说,零大小的物体:

Now you may think that an allocation of zero bytes couldn't possibly fail but the standard doesn't mandate that. It says, for zero-sized objects:

如果该空间的请求的大小为零,则该行为定义执行:返回任一个空指针,或行为是因为如果尺寸是一些非零值,除了返回的指针不应使用访问对象。

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

所以,如果你的实现是那些的一个没有的返回NULL一个零大小分配,它需要的部分的每个分配家务的信息,以及内存竞技场的这样的完整,不能存储甚至另一个看家头,并且它不是足够聪明,只是拆分当前分配和大部分回到赛场上,它可能会失败一个零大小的realloc ,因此不会释放原来的内存。

So, if your implementation is one of those that doesn't return NULL for a zero-size allocation, and it requires some housekeeping information for each allocation, and the memory arena is so full that you can't store even another housekeeping header, and it's not smart enough just to split the current allocation and return most of it to the arena, it may fail a zero-size realloc and hence not free the original memory.

现在,这是取决于很多的实施,其中大部分可能显得有些怪异的人有写他们的经验作出的决定(非常)边缘情况。不幸的是,我们必须允许边缘情况,以及: - )

Now this is a (very) edge case depending on a lot of decisions made in the implementation, most of which may seem bizarre to people with experience of writing them. Unfortunately, we have to allow for the edge cases as well :-)

如果它真的担心你,我只是改变:

If it really worries you, I'd just change:

p=(int *)realloc(p,0);

free (p) ; p = NULL;


一个其他几个要点。


A couple of other points.


  1. 注释marjers是斜杠 / ,而不是反斜杠。

  2. 您不允许在最后一行取消引用 P 您是否执行了零大小分配返回NULL。看到我上面在声明第二个引号:除了返回的指针不得用于访问对象

  3. 您真的不应该从的malloc 型呼叫转换返回值。这是朦胧黑暗的过去宿醉C的一个无效* 的malloc 返回的char * 。用这样时下,如果你忘了包括文件stdlib.h ,问题可以从你隐藏的问题。

  1. Comment marjers are forward slashes /, not backslashes.
  2. You are not allowed to dereference p in that final line whether or not your implementation returns NULL for a zero-size allocation. See my second quote above where it states: "except that the returned pointer shall not be used to access an object".
  3. You shouldn't really cast the return values from malloc-type calls. This is a hangover from the dim dark past before C had a void*, and malloc returned char*. The problem with doing so nowadays is, if you forget to include stdlib.h, problems can be hidden from you.

这篇关于malloc和realloc函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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