What malloc calloc function in c | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What malloc calloc function in c

11th Aug 2018, 7:21 AM
Nitesh Chand
Nitesh Chand - avatar
2 Answers
+ 2
Malloc allocates addresses of memory from any place (but that are closest to the first space), while Calloc uses contiguous spaces (eache space must be exactly side by side). So if you do lote of allocation processes and you are running out of memory, Malloc may be the best opition to choose. Another tip is to never miss to free(your_pointers)! Example: int *p = malloc(sizeof(int)*2); printf("%d %d", p, p+1); This code may print something like 1234 1246 but can also print something like 1234 1238. Altrough, If we wrote the same code but replaced malloc with calloc, the output would only be something like 1234 1238. As int uses 4 bits (or 8 in some machines), we need to count 4+4 instead of 1+1. https://code.sololearn.com/clfhJ0zvB2oc/?ref=app
30th Sep 2019, 3:04 AM
Aaron Stiebler Carneiro da Silva
Aaron Stiebler Carneiro da Silva - avatar
+ 1
https://www.geeksforgeeks.org/calloc-versus-malloc/ http://cs-fundamentals.com/tech-interview/c/difference-between-malloc-and-calloc.php https://stackoverflow.com/questions/1538420/difference-between-malloc-and-calloc Top 3 answers brought to you by... SEARCH ENGINES "Can you hear me now?"
11th Aug 2018, 7:41 AM
Janningā­
Janningā­ - avatar