R
ranjeet.gupta
Dear All
we all are familiar with the calloc and malloc, Now we know that
calloc does the initialisation which is not achived in the malloc
while allocatings bytes for diffrent data types.
Now my concern about these two are which is more efficent to use,
To avoid any mallacisous behaviour we gernally adopt the calloc,
anyway both the calloc and malloc allocates the bytes in continious
memeory location. (As calloc also initalises)
Now I suppose the malloc is more efficent with the calloc
reason behind this is that Calloc is having the extra overhead of
initailisaig the bytes with Zero (Default), when we just simply want
to allocate the bytes (no need to initialise it)
(char *) abcd = (char *)calloc (2, 100);
above is same as
(char *) abcd = (char *)malloc(200);
memset(abcd, 0, 1 * 200); // system called
Now this means that calloc is not so much efficient as compared to
malloc due to over head.
But see the below case that
suppose we have to allocte the memory for the int data type
and initialise it with Zero,
Case 1:
(int *) abcd = (int *)malloc(100);
memset(abcd, 0, sizeof(int) * 100); // The user called memset()
Case 2:
(int *) abcd = (int *)calloc(1, 100);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
above is equivelent to
(int *) abcd = (int *)malloc(100);
memset(abcd, 0, sizeof(int) * 100); // The system called memset()
So as in case 1 we have the User called memset fuction, hence
case 1 is inefficent with respect to the case 2, Thus making the case
2 more efficient.
Now Suppose we have the to allocate the memory for again int data
type and initialise it with 2,
Case A:
(int *) abcd = (int *)malloc(100);
memset(abcd, 2, sizeof(int) * 100); // The user called memset()
Case B:
(int *) abcd = (int *)calloc(1, 100);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
above is equivelent to
(int *) abcd = (int *)malloc(100);
memset(abcd, 0, sizeof(int) * 100); // The system called memset()
memset(abcd, 2, sizeof(int) * 100); // The usser called memset()
So we see that Case A is efficent as compared to the CASe B
Thus as In Case A only once the memset() is called, while in
case B two times memset() fucntion is called. (I.e Usser and system)
Thus Malloc is more efficent in this case.
So this make the diffrence in the terms of the Efficency of the calloc
and malloc,
where we have to get the default intialiosation then always use
calloc and where we have to use the Some specified intialisation then
always use the malloc.
Please Guide me as As I was trying to get the diffrence between the
calloc and malloc .. this is what i guessed it may be a diffrence
also. Please let me know m i correct or not or what i am missing.
Thanks In Advance
Ranjeet
we all are familiar with the calloc and malloc, Now we know that
calloc does the initialisation which is not achived in the malloc
while allocatings bytes for diffrent data types.
Now my concern about these two are which is more efficent to use,
To avoid any mallacisous behaviour we gernally adopt the calloc,
anyway both the calloc and malloc allocates the bytes in continious
memeory location. (As calloc also initalises)
Now I suppose the malloc is more efficent with the calloc
reason behind this is that Calloc is having the extra overhead of
initailisaig the bytes with Zero (Default), when we just simply want
to allocate the bytes (no need to initialise it)
(char *) abcd = (char *)calloc (2, 100);
above is same as
(char *) abcd = (char *)malloc(200);
memset(abcd, 0, 1 * 200); // system called
Now this means that calloc is not so much efficient as compared to
malloc due to over head.
But see the below case that
suppose we have to allocte the memory for the int data type
and initialise it with Zero,
Case 1:
(int *) abcd = (int *)malloc(100);
memset(abcd, 0, sizeof(int) * 100); // The user called memset()
Case 2:
(int *) abcd = (int *)calloc(1, 100);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
above is equivelent to
(int *) abcd = (int *)malloc(100);
memset(abcd, 0, sizeof(int) * 100); // The system called memset()
So as in case 1 we have the User called memset fuction, hence
case 1 is inefficent with respect to the case 2, Thus making the case
2 more efficient.
Now Suppose we have the to allocate the memory for again int data
type and initialise it with 2,
Case A:
(int *) abcd = (int *)malloc(100);
memset(abcd, 2, sizeof(int) * 100); // The user called memset()
Case B:
(int *) abcd = (int *)calloc(1, 100);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
above is equivelent to
(int *) abcd = (int *)malloc(100);
memset(abcd, 0, sizeof(int) * 100); // The system called memset()
memset(abcd, 2, sizeof(int) * 100); // The usser called memset()
So we see that Case A is efficent as compared to the CASe B
Thus as In Case A only once the memset() is called, while in
case B two times memset() fucntion is called. (I.e Usser and system)
Thus Malloc is more efficent in this case.
So this make the diffrence in the terms of the Efficency of the calloc
and malloc,
where we have to get the default intialiosation then always use
calloc and where we have to use the Some specified intialisation then
always use the malloc.
Please Guide me as As I was trying to get the diffrence between the
calloc and malloc .. this is what i guessed it may be a diffrence
also. Please let me know m i correct or not or what i am missing.
Thanks In Advance
Ranjeet