malloc()、calloc()、realloc()的用法

calloc(m, n) 本质上等价于
    p = malloc(m * n);
    memset(p, 0, m * n);
填充的零是全零, 因此不能确保生成有用的空指针值或浮点零值

free() 可以安全地用来释放 calloc() 分配的内存。

Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slightly different from the other. malloc() takes a size and returns a pointer to a chunk of memory at least that big:

void *malloc( size_t size );

malloc()和calloc()都是用来分配动态内存的函数,两者的操作有以下的区别:

malloc()以分配的内存大小size为参数返回一个指针,该指针指向一块最小值为size的内存区域。

calloc() takes a number of elements, and the size of each, and returns a pointer to a chunk of memory 
at least big enough to hold them all:void *calloc( size_t numElements, size_t sizeOfElement );

calloc的参数为一组元素和每个元素的size,返回的指针指向的内存至少可以存储所有的这些元素单元。

There are one major difference and one minor difference between the two functions. The major difference is that malloc() doesn't initialize the allocated memory. The first time malloc() gives you a particular chunk of memory, the memory might be full of zeros. If memory has been allocated, freed, and reallocated, it probably has whatever junk was left in it. That means, unfortunately, that a program might run in simple cases (when memory is never reallocated) but break when used harder (and when memory is reused). calloc() fills the allocated memory with all zero bits. That means that anything there you are going to use as a char or an int of any length, signed or unsigned, is guaranteed to be zero. Anything you are going to use as a pointer is set to all zero bits. That is usually a null pointer, but it is not guaranteed.Anything you are going to use as a float or double is set to all zero bits; that is a floating-point zero on some types of machines, but not on all.

两者间有一个主要和次要的区别。主要区别为malloc不初始化分配的内存区域。第一次调用malloc返回的内存可能初值为0,如果该内存被分配、释放然后又重新分配,那么存储区可能存储的就是残留的垃圾数据。也就是说,程序可能在内存不重新分配的简单情况下可以运行,不幸的是如果内存可以重分配,程序就会异常。calloc会初始化分配的内存为0,即你将使用的任何东西,不管是字符还是任何长度,有符号或无符号的整数都是0。你的指针也都指向为0的字节位。通常是一个空指针,但并不保证总是这样。浮点数和双精度型的数据也都是0,有的机器上有值为0的浮点数指针,但不普遍。

The minor difference between the two is that calloc() returns an array of objects; malloc() returns one object. Some people use calloc() to make clear that they want an array.

两者次要的区别为calloc返回一串对象,malloc返回一个对象,当强调想返回一个队列时,应该调用calloc。(别人翻译的)

--------------------------------------------------------------------------------------------
realloc 用过很多次了。无非就是将已经存在的一块内存扩大。

char* p = malloc(1024);
char* q = realloc(p,2048);

现在的问题是我们应该如何处理指针 p。 刚开始按照我最直观的理解,如果就是直接将 p = NULL;。 到最后只需要释放 q的空间就可以了。

因为最近在做个封装。结果在做单元测试的时候发现。有时候我在 free(q); 的时候会出错。这样我就郁闷了。

后来仔细一跟踪,发现 realloc 完以后 q 和 p 的指针地址是一样。不过有时候又不一样。

仔细查了下资料。得到如下信息:

       1.如果 当前连续内存块足够 realloc 的话,只是将p所指向的空间扩大,并返回p的指针地址。 这个时候 q 和 p 指向的地址是一样的。

       2.如果 当前连续内存块不够长度,再找一个足够长的地方,分配一块新的内存,q,并将 p指向的内容 copy到 q,返回 q。并将p所指向的内存空间删除。

这样也就是说 realloc 有时候会产生一个新的内存地址 有的时候不会。所以在分配完成后。我们需要判断下 p 是否等于 q。并做相应的处理。

这里有点要注意的是要避免 p = realloc(p,2048); 这种写法。有可能会造成 realloc 分配失败后,p原先所指向的内存地址丢失。

转载于:https://www.cnblogs.com/dreamsyeah/p/5878466.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值