Open
+ 4

Can anyone clearly explain about malloc() , calloc() , realloc() function???

29th May 2019, 4:48 PM
Bala Kumaran
Bala Kumaran - avatar
5 Answers
+ 15
Bala Kumaran calloc() Same principle as malloc(), but it's used to allocate storage. The real difference between these two, is that calloc() initializes all bytes in the allocation block to zero, because it's used to reserve space for dynamic arrays. It's written like this. source: https://medium.com/@jraleman/c-programming-language-functions-malloc-calloc-realloc-and-free-61cfc3e45da7 malloc() In C, the library function malloc is used to allocate a block of memory on the heap. The program accesses this block of memory via a pointer thatmalloc returns. When the memory is no longer needed, the pointer is passed to free which deallocates the memory so that it can be used for other purposes. source: wikipedia realloc() The C library function void *realloc(void *ptr, size_t size)  attempts to resize the memory block pointed to by ptr that was previously allocated with a call to malloc or calloc. source: https://www.tutorialspoint.com/c_standard_library/c_function_realloc.htm I hope I was helpful
29th May 2019, 4:52 PM
Alessio Benvenuti
Alessio Benvenuti - avatar
+ 3
Malloc- This just allocates the specified memory. Calloc- Allocates contiguous block of memory for something like array. realloc- Re allocates memory for the specified pointer.
29th May 2019, 4:54 PM
Akib
Akib - avatar
+ 3
dear devanille, can you explain memory management functions such as malloc(), calloc(), realloc & free () with example program
30th May 2019, 2:51 AM
Bala Kumaran
Bala Kumaran - avatar
+ 2
Those are three functions used for dynamic memory allocation (for especifying manually a contiguous block of memory). Malloc simply allocates a block of memory; Calloc also allocates a block of memory but clears that space to 0; Realloc is used for re-allocate a block of memory previously allocated (it is used for resize, but if the new resized block does not fit in the current position, must be re-allocated to another one).
29th May 2019, 5:57 PM
devanille
devanille - avatar
+ 2
Bala Kumaran of course. I think you could find good (or maybe better) examples out there by more skilled programmers, but I'll try my best. If you have any doubt with the following code you can ask and I'll try to explain in more deep. Hope it helps... https://code.sololearn.com/cAK1JGqqM0hi/?ref=app
30th May 2019, 6:24 AM
devanille
devanille - avatar