Why we use malloc () and calloc () in the programs. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Why we use malloc () and calloc () in the programs.

C language

16th Apr 2019, 6:03 AM
Deepak Sharma
Deepak Sharma - avatar
6 Answers
+ 3
They are used to dynamically allocate memory on the heap, which is a much larger memory pool than static allocated memory. Malloc takes one parameter(size of the memory), allocates it but leaves it uninitialized(garbage values). Calloc takes two arguments (number of blocks to allocate and size of each block), allocates it and initialize all of it to 0.
16th Apr 2019, 9:10 AM
Rasmus Bons
Rasmus Bons - avatar
+ 3
When you write code, try not to leave any space between the malloc/calloc keywords and the parentheses if you want to avoid syntax errors.
17th Apr 2019, 2:40 AM
Sonic
Sonic - avatar
0
But we can use malloc/calloc for memory is use to save or memory allocation.
17th Apr 2019, 2:46 AM
Deepak Sharma
Deepak Sharma - avatar
0
But why
17th Apr 2019, 6:50 AM
Deepak Sharma
Deepak Sharma - avatar
0
Because you are not always sure what amount of memory you will use at compile time. Therefore it can be useful to be able to dynamically allocate memory
17th Apr 2019, 7:31 AM
Rasmus Bons
Rasmus Bons - avatar
- 1
Not sure what you mean by that
17th Apr 2019, 6:33 AM
Rasmus Bons
Rasmus Bons - avatar