belongs to collection: 10 Questions on dynamic memory allocation in C
Is it better to use malloc () or calloc ()?
Answer:
The calloc function initialize the allocated memory with 0 but malloc don’t. So the memory which is allocated by the malloc has the garbage data. In another word you can say that calloc is equal to the combination of malloc and memeset.
See the below expression,
ptr = calloc(nmember, size); //is essentially equivalent to ptr = malloc(nmember * size); memset(ptr, 0, (nmember * size));
Note: If you don’t want to initialize the allocated memory with zero, It would be better to use malloc over calloc.
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Answer:
The calloc function initialize the allocated memory with 0 but malloc don’t. So the memory which is allocated by the malloc has the garbage data. In another word you can say that calloc is equal to the combination of malloc and memeset.
See the below expression,
Note: If you don’t want to initialize the allocated memory with zero, It would be better to use malloc over calloc.
need an explanation for this answer? contact us directly to get an explanation for this answer