C++中malloc和realloc的意义

realloc

       1. realloc失败的时候,返回NULL
  2. realloc失败的时候,原来的内存不改变,不会释放也不会移动
  3. 假如原来的内存后面还有足够多剩余内存的话,realloc的内存=原来的内存+剩余内存,realloc还是返回原来内存的地址; 假如原来的内存后面没有足够多剩余内存的话,realloc将申请新的内存,然后把原来的内存数据拷贝到新内存里,原来的内存将被free掉,realloc返回新内存的地址
  4. 如果size为0,效果等同于free()
  5. 传递给realloc的指针必须是先前通过malloc(), calloc(), 或realloc()分配的
  6.传递给realloc的指针可以为空,等同于malloc。
malloc

Others have answered how malloc(0) works. I will answer one of the questions that you asked that hasn't been answered yet (I think). The question is about realloc(malloc(0), 0):

What does malloc(0) return? Would the answer be same for realloc(malloc(0),0)?

The standard says this about realloc(ptr, size):

  • if ptr is NULL, it behaves like malloc(size),
  • otherwise (ptr is not NULL), it deallocates the old object pointer to by ptr and returns a pointer to a new allocated buffer. But if size is 0, C89 says that the effect is equivalent to free(ptr). Interestingly, I can't find that statement in C99 draft (n1256 or n1336). In C89, the only sensible value to return in that case would be NULL.

So, there are two cases:

  • malloc(0) returns NULL on an implementation. Then your realloc() call is equivalent torealloc(NULL, 0). That is equivalent to malloc(0) from above (and that is NULL in this case).
  • malloc(0) returns non-NULL. Then, the call is equivalent to free(malloc(0)). In this case,malloc(0) and realloc(malloc(0), 0) are not equivalent.

Note that there is an interesting case here: in the second case, when malloc(0) returns non-NULLon success, it may still return NULL to indicate failure. This will result in a call like: realloc(NULL, 0), which would be equivalent to malloc(0), which may or may not return NULL.

I am not sure if the omission in C99 is an oversight or if it means that in C99, realloc(ptr, 0) for non-NULL ptr is not equivalent to free(ptr). I just tried this with gcc -std=c99, and the above is equivalent to free(ptr).

Edit: I think I understand what your confusion is:

Let's look at a snippet from your example code:

ptr = malloc(0);
if (ptr == realloc(ptr, 1024))

The above is not the same as malloc(0) == realloc(malloc(0), 1024). In the second, themalloc() call is made twice, whereas in the first, you're passing a previously allocated pointer torealloc().

Let's analyze the first code first. Assuming malloc(0) doesn't return NULL on success, ptr has a valid value. When you do realloc(ptr, 1024)realloc() basically gives you a new buffer that has the size 1024, and the ptr becomes invalid. A conforming implementation may return the same address as the one already in ptr. So, your if condition may return true. (Note, however, looking at the value of ptr after realloc(ptr, 1024) may be undefined behavior.)

Now the question you ask: malloc(0) == realloc(malloc(0), 1024). In this case, let's assume that both the malloc(0) on the LHS and RHS returns non-NULL. Then, they are guaranteed to be different. Also, the return value from malloc() on the LHS hasn't been free()d yet, so any othermalloc()calloc(), or realloc() may not return that value. This means that if you wrote your condition as:

if (malloc(0) == realloc(malloc(0), 1024)
    puts("possible");

you won't see possible on the output (unless both malloc() and realloc() fail and returnNULL).

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    void *p1;
    void *p2;

    p1 = malloc(0);
    p2 = realloc(p1, 1024);
    if (p1 == p2)
        puts("possible, OK");

    /* Ignore the memory leaks */
    if (malloc(0) == realloc(malloc(0), 1024))
        puts("shouldn't happen, something is wrong");
    return 0;
}

On OS X, my code didn't output anything when I ran it. On Linux, it prints possible, OK.


  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
malloc、calloc、realloc和new都是C++和C语言动态分配内存时经常用到的函数。它们之间的区别如下: 1. malloc malloc是C语言最基本的动态内存分配函数。它需要传入分配内存空间的大小,并返回分配空间的首地址。malloc分配内存时,只是在内存空间找到大小合适的连续空闲区域,然后将该内存块标记为已用状态。使用完毕后,需要手动调用free()函数来释放所分配的内存空间,否则将会造成内存泄漏。 2. calloc calloc与malloc类似,它也是用来分配内存的。calloc需要传入分配内存空间的大小和空间个数,并返回分配空间的首地址。calloc分配出来的内存空间是没有被初始化的,即内存的每一位都被初始化为0。使用完毕后,同样需要调用free()函数来释放空间。 3. realloc realloc是C/C++用来重新调整内存大小的函数。它需要传入要调整内存的地址和要调整的大小。如果新的内存大小比原来的内存大小小,则realloc将原有内存块缩小为新的大小,多余的空间将释放;如果新的内存大小比原来的内存大小大,则需要重新分配一块内存,将原有内存块的数据拷贝到新的内存块,并释放原有内存块。使用完毕后,仍需要调用free()函数来释放内存。 4. new new是C++的动态内存分配方式,它是一个运算符,不是一个函数。new可以分配任意类型的内存空间,包括基本类型、类、结构体等。使用new分配内存后,不需要手动释放内存,因为C++有自动的内存回收机制。如果使用new分配的空间已经不需要使用,C++会自动调用delete或delete[]来释放内存。 总的来说,malloc、calloc、realloc和new都是动态内存分配的方式,它们的使用场景不同,需要根据具体情况选择使用。在使用完毕后,都需要手动或自动释放内存,以避免内存泄漏的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值