malloc和calloc区别
来源:互联网 发布:沈阳关键字优化公司 编辑:程序博客网 时间:2024/04/28 08:08
Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slightly different from the other. calloc() takes a number of elements, and the size of each, and returns a pointer to a chunk of memory
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 );
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 );
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.
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.
- malloc和calloc区别
- calloc 和 malloc 区别
- MALLOC和CALLOC区别
- malloc()和calloc()区别
- malloc和calloc区别
- malloc和calloc的区别
- malloc 和 calloc的区别?
- calloc和malloc区别、分析
- malloc和calloc的区别
- malloc和 calloc的区别
- malloc和calloc的区别
- malloc()和calloc()的区别
- malloc、calloc和realloc区别
- malloc和calloc的区别
- malloc和calloc的区别
- malloc和calloc的区别和联系
- malloc()和calloc()的区别及calloc 实现
- malloc和calloc区别(c)
- 报纸终于改版了
- 使用fcitx修复linux下java程序不能使用iiim输入法的错误
- PHP.MVC的模板标签系统(三)
- SUN,UNIX和微软的避孕套
- JavaScript数值问题
- malloc和calloc区别
- spring-richclient开发swing应用程序
- my new blog
- spring-richclient开发swing应用程序 2
- spring-richclient开发swing应用程序 3
- spring-richclient开发swing应用程序 4
- 分布式计算----JAVA技术
- vbscript和javascript互相调用
- javascript的一些东西