Aspects


vs


Calloc

The calloc function performs dynamic memory allocation in C, and is part of the standard library.

Malloc

The malloc function performs dynamic memory allocation in C and is part of the standard library

Speed likely page

Quality Example
Alone faster

"And is as far as i know faster than the combination of malloc and memset on the other hand malloc alone is faster than calloc"

from question "Is the memory chunk returned by malloc (and its cousins) initialized to Zero?"

Better

"It takes a page off of the free_page_list updates mem_map zeroes the page and returns the physical address of the page. here s another post that explains it well and also explains why using calloc is better than malloc + memset"

from question "Is the memory chunk returned by malloc (and its cousins) initialized to Zero?"

Slower

"Calloc itself is slower than malloc because you have to spend some time to clear the contents of allocated memory"

from question "C - calloc() v. malloc()"

"I remember somewhere i have read that calloc is slower than malloc because calloc performs initialization to zero after performing memory allocation"

from question "Initializing to zero after malloc or calling calloc"

"Also calloc is slower than malloc from operating system memory allocation perspective"

from question "Malloc and calloc"

Longer

"Calloc does take longer than malloc because it has to zero out all the memory you asked for before giving it to you"

from question "Why is my simple C program displaying garbage to stdout?"

Faster

"It would be better to use malloc over calloc unless we want the zero-initialization because malloc is faster than calloc"

from question "* (no title is found for this review)"

"Calloc is faster than malloc + memset because calloc knows that the mmap d pages are pre-zeroed and memset forces the allocation of physical ram"

from question "Question Regarding Dynamic memory allocation"

"It s conceivable that calloc could return address of memory location that is already pre-initialized with zeros thus it may be faster than malloc + memset combo"

from question "Memset on float array does not fully zero out array"

"In terms of speed calloc is likely to be faster than malloc + memset if memory needs to be zeroed out"

from question "What's the difference between calloc & malloc followed by a memset?"

"This means calloc can potentially be faster than calling malloc followed by memset since it can skip the memset if it knows it will already by zeroed"

from question "Who zeroes pages while calling calloc() in Linux?"

"Malloc is faster since calloc initializes the allocated memory to contain all zeros"

from question "C - calloc() v. malloc()"

"This is an enormous amount of extra work and explains why calloc is faster than malloc and memset"

from question "Why malloc+memset is slower than calloc?"

Still faster

"If end up using the memory anyway calloc is still faster than malloc and memset but the difference is not quite so ridiculous"

from question "Why malloc+memset is slower than calloc?"

Others

Quality Example
More

"Edit i m not a java expert but i think that in java members of new array are initialized to 0 or null so calloc is more correct than malloc in my code"

from question "Questions about the multi-dimension array in Java and C#?"

Less arguments

"G_new and g_new0 both take the same arguments unlike malloc that takes less arguments than calloc"

from question "Do I cast the result of malloc?"

Far more

"Malloc is far more common in c code than calloc"

from question "Is there there any reason NOT to use calloc instead of malloc?"

Larger blocks

"Those answers was that calloc can allocate larger blocks than malloc can and etc"

from question "Why does calloc require two parameters and malloc just one?"

Better

"As written malloc would be better than calloc but the question used calloc and it would not be hard to make it sensible for use with this code too for example a conditional assignment in set_matrix such as"

from question "How to create variable length array on heap?"

Closer

"Actually calling memset after malloc is closer to calling calloc than the option you suggest"

from question "Is malloc + strcpy function as good as calloc?"

Bigger blocks

"For this reason since calloc uses two arguments of type size_t it can allocate bigger blocks than malloc will ever be able to since malloc takes only one argument of type size_t"

from question "Two arguments to calloc"

Faster

"Malloc is faster than calloc reason is that malloc processed single dimensional array to pointer format whereas calloc takes double dimensional array and before processed it converts to single dimensional array then to pointer format"

from question "Calloc v/s malloc and time efficiency"

Back to Home
Data comes from Stack Exchange with CC-BY-SA-3.0