区别是什么:
ptr = malloc(MAXELEMS * sizeof(char *));
And:
ptr = calloc(MAXELEMS, sizeof(char*));
什么时候使用calloc优于malloc或反之亦然?
区别是什么:
ptr = malloc(MAXELEMS * sizeof(char *));
And:
ptr = calloc(MAXELEMS, sizeof(char*));
什么时候使用calloc优于malloc或反之亦然?
当前回答
Calloc一般是malloc+memset为0
显式使用malloc+memset通常会稍微好一点,特别是当你在做以下事情时:
ptr=malloc(sizeof(Item));
memset(ptr, 0, sizeof(Item));
That is better because sizeof(Item) is know to the compiler at compile time and the compiler will in most cases replace it with the best possible instructions to zero memory. On the other hand if memset is happening in calloc, the parameter size of the allocation is not compiled in in the calloc code and real memset is often called, which would typically contain code to do byte-by-byte fill up until long boundary, than cycle to fill up memory in sizeof(long) chunks and finally byte-by-byte fill up of the remaining space. Even if the allocator is smart enough to call some aligned_memset it will still be a generic loop.
一个值得注意的例外是,当您对一个非常大的内存块(一些power__2kb)执行malloc/calloc时,在这种情况下,可以直接从内核进行分配。由于操作系统内核通常会出于安全原因将它们放弃的所有内存归零,足够聪明的calloc可能只返回内存,而不进行额外的归零。同样,如果你只是分配一些你知道很小的东西,那么在性能方面使用malloc+memset可能会更好。
其他回答
calloc的一个经常被忽视的优点是,它将帮助保护您免受整数溢出漏洞的侵害。比较:
size_t count = get_int32(file);
struct foo *bar = malloc(count * sizeof *bar);
vs.
size_t count = get_int32(file);
struct foo *bar = calloc(count, sizeof *bar);
如果count大于SIZE_MAX/sizeof *bar,前者可能导致少量分配和后续缓冲区溢出。在这种情况下,后者将自动失败,因为无法创建如此大的对象。
当然,您可能不得不注意不符合规范的实现,这些实现简单地忽略了溢出的可能性……如果在您的目标平台上存在这个问题,那么无论如何都必须对溢出进行手动测试。
区别1:
Malloc()通常分配内存块,它是初始化的内存段。
Calloc()分配内存块并将所有内存块初始化为0。
区别2:
如果考虑malloc()语法,它只需要1个参数。考虑下面的例子:
data_type ptr = (cast_type *)malloc( sizeof(data_type)*no_of_blocks );
例如:如果你想为int类型分配10块内存,
int *ptr = (int *) malloc(sizeof(int) * 10 );
如果考虑calloc()语法,它将接受2个参数。考虑下面的例子:
data_type ptr = (cast_type *)calloc(no_of_blocks, (sizeof(data_type)));
例如:如果你想为int类型分配10块内存,并将所有这些初始化为0,
int *ptr = (int *) calloc(10, (sizeof(int)));
相似度:
malloc()和calloc()如果没有进行类型强制转换,默认情况下都会返回void* !
malloc和calloc都分配内存,但calloc将所有位初始化为0,而malloc则不这样做。
可以说,Calloc相当于malloc + memset + 0(其中memset将指定的内存位设置为0)。
因此,如果不需要初始化为0,那么使用malloc可能会更快。
malloc()和calloc()是来自C标准库的函数,它们允许动态内存分配,这意味着它们都允许在运行时分配内存。
他们的原型如下:
void *malloc( size_t n);
void *calloc( size_t n, size_t t)
两者的区别主要有两点:
行为:malloc()分配一个内存块,不初始化它,从这个块中读取内容将导致垃圾值。另一方面,Calloc()分配一个内存块并将其初始化为0,显然读取这个块的内容将导致为0。 语法:malloc()有一个参数(要分配的大小),calloc()有两个参数(要分配的块的数量和每个块的大小)。
如果成功,两者的返回值都是指向已分配内存块的指针。否则返回NULL,表示内存分配失败。
例子:
int *arr;
// allocate memory for 10 integers with garbage values
arr = (int *)malloc(10 * sizeof(int));
// allocate memory for 10 integers and sets all of them to 0
arr = (int *)calloc(10, sizeof(int));
使用malloc()和memset()可以实现与calloc()相同的功能:
// allocate memory for 10 integers with garbage values
arr= (int *)malloc(10 * sizeof(int));
// set all of them to 0
memset(arr, 0, 10 * sizeof(int));
注意malloc()最好比calloc()使用,因为它更快。如果需要对值进行零初始化,则使用calloc()。
在<stdlib.h>标头中声明的calloc()函数比malloc()函数提供了几个优点。
它将内存分配为一定数量的给定大小的元素 它初始化所分配的内存,这样所有的位都是 零。