区别是什么:
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或反之亦然?
当前回答
块数: Malloc()分配请求的单个内存块, Calloc()为请求的内存分配多个块
初始化: Malloc() -不清除和初始化分配的内存。 Calloc() -将分配的内存初始化为0。
速度: Malloc()速度很快。 Calloc()比malloc()慢。
参数和语法: Malloc()接受1个参数:
字节 要分配的字节数
Calloc()有两个参数:
长度 要分配的内存块的数量 字节 在每个内存块上分配的字节数
void *malloc(size_t bytes);
void *calloc(size_t length, size_t bytes);
内存分配方式: malloc函数从可用堆中分配所需“大小”的内存。 calloc函数分配的内存大小等于' num *size '。
名称含义: malloc的意思是“内存分配”。 calloc的意思是“连续分配”。
其他回答
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只是用物理全零位模式填充内存块。在实践中,通常假设位于用calloc分配的内存块中的对象具有初始值,就像它们是用文字0初始化的一样,即整数的值应该是0,浮点变量的值应该是0.0,指针的值应该是适当的空指针值,等等。
然而,从学究的角度来看,calloc(以及memset(…, 0,…))只能保证正确地初始化unsigned char类型的对象(使用0)。其他所有内容都不能保证被正确初始化,并且可能包含所谓的陷阱表示,这会导致未定义的行为。换句话说,对于除unsigned char以外的任何类型,前面提到的全零位模式可能表示非法值,即陷阱表示。
后来,在C99标准的一个技术更正中,为所有整数类型定义了行为(这是有意义的)。也就是说,在当前的C语言中,你只能用calloc(和memset(…, 0,…))。从C语言的角度来看,在一般情况下使用它来初始化其他任何东西都会导致未定义的行为。
在实践中,calloc工作,我们都知道:),但是否想要使用它(考虑到上面的问题)取决于你。我个人更倾向于完全避免它,而是使用malloc并执行自己的初始化。
最后,另一个重要的细节是,calloc需要在内部计算最终的块大小,通过将元素大小乘以元素数量。在执行此操作时,calloc必须监视可能的算术溢出。如果无法正确计算请求的块大小,将导致分配不成功(空指针)。同时,您的malloc版本不会尝试监视溢出。它将分配一些“不可预测”的内存数量,以防发生溢出。
摘自Georg Hager的博客上的一篇文章,用calloc()进行有趣的基准测试
When allocating memory using calloc(), the amount of memory requested is not allocated right away. Instead, all pages that belong to the memory block are connected to a single page containing all zeroes by some MMU magic (links below). If such pages are only read (which was true for arrays b, c and d in the original version of the benchmark), the data is provided from the single zero page, which – of course – fits into cache. So much for memory-bound loop kernels. If a page gets written to (no matter how), a fault occurs, the “real” page is mapped and the zero page is copied to memory. This is called copy-on-write, a well-known optimization approach (that I even have taught multiple times in my C++ lectures). After that, the zero-read trick does not work any more for that page and this is why performance was so much lower after inserting the – supposedly redundant – init loop.
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()。
The documentation makes the calloc look like malloc, which just does zero-initialize the memory; this is not the primary difference! The idea of calloc is to abstract copy-on-write semantics for memory allocation. When you allocate memory with calloc it all maps to same physical page which is initialized to zero. When any of the pages of the allocated memory is written into a physical page is allocated. This is often used to make HUGE hash tables, for example since the parts of hash which are empty aren't backed by any extra memory (pages); they happily point to the single zero-initialized page, which can be even shared between processes.
任何对虚拟地址的写都被映射到一个页,如果该页是零页,则分配另一个物理页,将零页复制到那里,并将控制流返回给客户端进程。这与内存映射文件、虚拟内存等工作方式相同。它使用分页。
下面是一个关于这个主题的优化故事: http://blogs.fau.de/hager/2007/05/08/benchmarking-fun-with-calloc-and-zero-pages/