+ 2

I don't quit understand the difference between malloc and calloc since both allocate memory contiguously

2nd Feb 2022, 9:41 PM
Brian Kimathi
Brian Kimathi - avatar
3 odpowiedzi
+ 5
Brian Kimathi malloc only allocates memory and does not change the garbage values that it contains, whereas calloc allocates and clears the memory by filling with 0. Use malloc when the program will fill the memory afterward. Use calloc when you need the memory cleared from garbage values immediately.
2nd Feb 2022, 10:59 PM
Brian
Brian - avatar
+ 3
malloc allocates a chunk of memory. calloc allocates a chunk of memory and sets all it's bytes to zero (no garbage values). use realloc for changing the allocated chunk's size (it will copy your data, and extend or shrink the size of allocated memory as you specify).
2nd Feb 2022, 10:56 PM
Mafdi
Mafdi - avatar