calloc与malloc的区别
2007-03-29 15:23
239 查看
都是动态分配内存。
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 ); // 分配元素的个数和每个元素的大小
主要的不同是malloc不初始化分配的内存,calloc初始化已分配的内存为0。
There are one major difference and one minor difference between the two functions. The major difference is that malloc() doesnt 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.
次要的不同是calloc返回的是一个数组,而malloc返回的是一个对象。
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低,如果没有初始为0的要求就用malloc。
这里是“calloc函数的效率(calloc与malloc+memset效率的对比)”
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 ); // 分配元素的个数和每个元素的大小
主要的不同是malloc不初始化分配的内存,calloc初始化已分配的内存为0。
There are one major difference and one minor difference between the two functions. The major difference is that malloc() doesnt 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.
次要的不同是calloc返回的是一个数组,而malloc返回的是一个对象。
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低,如果没有初始为0的要求就用malloc。
这里是“calloc函数的效率(calloc与malloc+memset效率的对比)”
相关文章推荐
- malloc、realloc和calloc的区别
- malloc&calloc的区别
- malloc、calloc、realloc的区别
- malloc()与calloc区别
- realloc,malloc,calloc三者的区别
- HNWE T2 malloc、calloc区别 分配在哪里
- realloc,malloc,calloc的区别
- malloc()和calloc()的区别及calloc 实现
- malloc与calloc的区别
- C/C++中的malloc、calloc与new的区别
- malloc()与calloc区别
- realloc,malloc,calloc的区别
- malloc、calloc、realloc的区别
- _alloc/malloc/calloc 内存分配的区别
- malloc 和alloc及calloc的区别
- malloc()和calloc()的区别
- c语言中malloc、realloc与calloc 的区别以及联系
- malloc()与calloc区别
- ralloc、calloc和malloc的用法、区别。实现原理
- malloc、calloc、realloc、alloca几个内存分配函数的区别