2

I know this question may be marked as a duplicate of difference between malloc and calloc but still i would like to ask it.

i know calloc initilizes the memory block,here my question is not focusussing on that part.

my question is

the definition of malloc says it allocates a block of memory of specified size.

and calloc says it allocates multiple block of memory ,each of the same size.

is this allocation of one block of memory and multiple blocks of memory is a real difference between the two?

because i feel we can do the same using malloc which can be done by calloc.

for example :

int *ptr;
ptr=(int *) malloc(100 * (sizeof(int)));

and

int *ptr;
ptr=(int *) calloc(100,sizeof(int));

would end up allocating 100 times the memory required by the int.

Burhan Ali
  • 2,258
  • 1
  • 28
  • 38
haris
  • 2,003
  • 4
  • 25
  • 24

7 Answers7

7

calloc fills the memory with ZERO's.

p=calloc(n, m); 

is equivalent to

p=malloc(n*m); 
memset(p, 0, m * n);

Thus, if you intend to set your allocated memory to zero, then using malloc you have to compute n*m twice, or use a temp variable, which is what calloc does.

Edit: I've just read the ISO C standard and found that nowhere is specified that the implementation of calloc should check if n*m overflows, that is, if it exceeds the constant SIZE_MAX in the C99 standard.

vulkanino
  • 9,074
  • 7
  • 44
  • 71
  • so other than initilizing to zero there is no other difference between the two? – haris Feb 09 '12 at 15:05
  • as I said, memory wise they are equivalent. – vulkanino Feb 09 '12 at 15:06
  • The equivalence isn't 100%, for two reasons. First, there's the pitfall of `n*m` overflow. `calloc` will fail if the product is too large, `malloc` just allocate less. Second, `calloc` may be faster than `malloc`+`memset` (but slower than just `malloc`). – ugoren Feb 09 '12 at 15:33
  • First, agree. Second, I never said the opposite! – vulkanino Feb 09 '12 at 16:23
  • @urogen: i don't see how malloc(n) - or any allocator - can allocate *less* than 'n' bytes ?? if it cannot allocate the amount requested, it should just fail – kaiwan Feb 07 '18 at 05:50
6

You are correct with your code examples ... the actual memory that is being pointed to by ptr is going to be the same size (i.e., and array on the heap of 100 int objects). As others have mentioned though, the call to calloc will actually zero-out that memory, where-as malloc will simply give you a pointer to that memory, and the memory may or may not have all zeroes in it. For instance, if you get memory that had been recycled from another object, then the call to malloc will still have the values from its previous use. Thus if you treat the memory as if it was "clean", and don't initialize it with some default values, you're going to end up with some type of unexpected behavior in your program.

Jason
  • 31,834
  • 7
  • 59
  • 78
4

Yes the main difference is mentioned above. Also calloc() is slower than malloc() from operating system memory allocation perspective.

The malloc() returns pointer doesn't touch the real memory until the program touches malloc(). Whereas calloc() back's the memory with RAM.

Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
  • 1
    :can you elaborate the second part – haris Feb 09 '12 at 15:31
  • 1
    I don't think the second part is right, at least not on any modern protected-memory OS like Linux/Unix/Windows ... can you explain further what you mean just in case I'm mis-understanding something? – Jason Feb 09 '12 at 15:43
  • Operating systems like Linux follows an optimistic memory allocation strategy. So pointer returned by malloc is not backed by physical memory until program uses that memory. From hardware point of view, they are 2 pages namely Anonymous and File-backed pages. We get anonymous page when we do malloc(). Anonymous pages may read swap area and for calloc() the OS must also find a suitable memory area by possibly swapping out other processes which makes it slower. – Sunil Bojanapally Feb 09 '12 at 16:00
  • +1 Thanks for the explanation, although you may want to change the wording in your second paragraph since it's a bit confusing how you are stating it ... maybe you should explicitly mention "Because of optimistic memory allocation at the OS level, ...", etc. – Jason Feb 09 '12 at 17:00
3

Well calloc also initializes the memory block to contain zeroes unlike malloc.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
3

This has been mentioned previously on this site, but judging from the other answers, I think it is worth repeating; multiplying two integers may result in overflow, and if that happens,

ptr = malloc(num*size);

will probably not have the desired result (and most likely result in a later segmentation fault). For these situations, calloc(num,size) should be preferred (though you could also test for overflow before calling malloc, if the fact that calloc() initializes the newly allocated memory to zero bothers you).

0

the main difference between the two is calloc initializes the memory blocks to zero while malloc created memory blocks contains garbage value.

so it is more suitable to use calloc instead of malloc to avoid uncertainity in your code.

0

===You can see the following difference for mallac() and calloc() function===

Initialization:
malloc() doesn't clear and initialize the allocated memory.
calloc() initialize the allocated memory by zero.

Syntex:

void *malloc(size_t size);                   // syntex for malloc() function
void *calloc(size_t num, size_t size);       // syntex for calloc() function

// example
ptr = malloc(num*size);   // for malloc() function
ptr = calloc(num,size);   // for calloc() function

Argument:
If you consider malloc() syntax, it will take only 1 argument.
If you consider calloc() syntax, it will take 2 arguments.

Manner of memory Allocation::
malloc() function assigns memory of the desired 'size' from the available heap.
calloc() function assigns memory that is the size of what’s equal to ‘num *size’.

Majbah Habib
  • 8,058
  • 3
  • 36
  • 38