Ask a Question

Difference between malloc and calloc


Spooky

on 2017-05-25 18:11:42  

calloc is for Continuous Allocation -> dynamically created memory is in continuous locations of RAM malloc is for Mixed Allocation -> dynamically created memory may or may not be on continuous locations of RAM. Example - consider creating a char array of 1 byte per char, addresses of array indices will be like this in calloc - 2000, 2001, 2002, 2003..... (continuous location in byte addressable RAM) in malloc - 2045, 2001, 2022, 3003..... (random values based on available free locations in RAM ) Its not that memory allocation will always be non-continuous in malloc. It may be that both malloc and calloc will assign the same address space in some cases, totally dependent on available free space in RAM