What is the difference between doing:
ptr = (char **) malloc (MAXELEMS * sizeof(char *));
or:
ptr = (char **) calloc (MAXELEMS, sizeof(char*));
When is it a good idea to use calloc over malloc or vice versa?
What is the difference between doing:
ptr = (char **) malloc (MAXELEMS * sizeof(char *));
or:
ptr = (char **) calloc (MAXELEMS, sizeof(char*));
When is it a good idea to use calloc over malloc or vice versa?
just allocates
n bytes
of memory without any initialization (ie; those memory bytes will contain any garbage values).However,
calloc()
method in c does the initialization as0
value for all the occupied memory bytes in addition to the function thatmalloc()
does.But apart from that, there is a very important difference. While calling
malloc(x)
it will allocate the memory (equals to x blocks) and return the pointer to the first byte allocated. However, it will not verify if exactly x blocks of memory are allocated. This will lead to the case of memory overflow. Howevercalloc()
verifies the allocation size. If it fails in the allocation of memory or verification of allocated bytes, it will simply return null.A less known difference is that in operating systems with optimistic memory allocation, like Linux, the pointer returned by
malloc
isn't backed by real memory until the program actually touches it.calloc
does indeed touch the memory (it writes zeroes on it) and thus you'll be sure the OS is backing the allocation with actual RAM (or swap). This is also why it is slower than malloc (not only does it have to zero it, the OS must also find a suitable memory area by possibly swapping out other processes)See for instance this SO question for further discussion about the behavior of malloc
from an article Benchmarking fun with calloc() and zero pages on Georg Hager's Blog
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 abstact 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.
Any write to virtual address is mapped to a page, if that page is the zero-page, another physical page is allocated, the zero page is copied there and the control flow is returned to the client process. This works same way memory mapped files, virtual memory, etc. work.. it uses paging.
Here is one optimization story about the topic: http://blogs.fau.de/hager/2007/05/08/benchmarking-fun-with-calloc-and-zero-pages/
There are two differences.
First, is in the number of arguments.
malloc()
takes a single argument (memory required in bytes), whilecalloc()
needs two arguments.Secondly,
malloc()
does not initialize the memory allocated, whilecalloc()
initializes the allocated memory to ZERO.calloc()
allocates a memory area, the length will be the product of its parameters.calloc
fills the memory with ZERO's and returns a pointer to first byte. If it fails to locate enough space it returns aNULL
pointer.Syntax:
ptr_var=(cast_type *)calloc(no_of_blocks , size_of_each_block);
i.e.ptr_var=(type *)calloc(n,s);
malloc()
allocates a single block of memory of REQUSTED SIZE and returns a pointer to first byte. If it fails to locate requsted amount of memory it returns a null pointer.Syntax:
ptr_var=(cast_type *)malloc(Size_in_bytes);
Themalloc()
function take one argument, which is the number of bytes to allocate, while thecalloc()
function takes two arguments, one being the number of elements, and the other being the number of bytes to allocate for each of those elements. Also,calloc()
initializes the allocated space to zeroes, whilemalloc()
does not.Difference 1: malloc() usually allocates the memory block and it is initialized memory segment. calloc() allocates the memory block and initialize all the memory block to 0.
Difference 2: If you consider malloc() syntax, it will take only 1 argument. Consider the following example below:
data_type ptr = (cast_type *)malloc( sizeof(data_type)*no_of_blocks );
Ex: If you want to allocate 10 block of memory for int type,
If you consider calloc() syntax, it will take 2 arguments. Consider the following example below:
data_type ptr = (cast_type *)calloc(no_of_blocks, (sizeof(data_type)));
Ex: if you want to allocate 10 blocks of memory for int type and Initialize all that to ZERO,
Similarity:
Both malloc() and calloc() will return void* by default if they are not type casted .!