Dynamic Memory Management
C provides three functions as its interface for dynamic memory allocation,
free. For most purposes,
calloc are basically the same. Dynamic memory management is used to allocate or free memory at runtime or execution time. Memory allocated from Heap memory section as per requirement. Library function
malloc is used to allocate a block of memory on the heap. The program accesses this block of memory using pointer. When the memory is no longer needed, the pointer is passed to
free function which deallocates the memory so that it can be used for other purposes.
Some languages provide a garbage collector to automatically handle deallocation of dynamically allocated memory, while other languages require the programmer to explicitly call memory deallocation facilities. Failure to deallocate dynamically allocated memory commonly results in memory leaks. The C dynamic memory allocation functions malloc, calloc and free are declared
stdlib.h header file.
void* malloc (size_t size);
It allocates memory block of given size in bytes and returns a void type pointer to the beginning of the block. this pointer need to be type cast to required data type. malloc() does not initialize the allocated memory, so these elements holds garbage data.
To allocate memory for 5 int type element, i.e dynamic array of 5 elements
int *p = (int *) malloc(sizeof(int)*5);
p is integer type pointer,
malloc functions allocates memory block of specified size. here we require 5 integer type elements, so using
sizeof operator find out bytes require for integer type element and multiply by number of elements required, which is 5. the total memory allocated is 5* size of integer variable which is eihter 2 bytes or 4 bytes, hence total size of memory block is 10 or 20 bytes. malloc function returns a pointer to an area of memory. If the space is insufficient, allocation fails and returns NULL pointer.
void* calloc (size_t num, size_t size);.
It accepts two arguments, number of elements and size of variable. calloc allocates memory block of given size in bytes and returns a void type pointer to the beginning of the block. this pointer need to be type cast to required data type. calloc() initialize the allocated memory to zero.
void *realloc(void *ptr, size_t size)
realloc function resize the memory block pointed to by ptr that was previously allocated with a malloc or calloc function. It accepts two parameter, ptr is pointer to a memory block which is previously allocated with malloc, calloc. If this is NULL, a new block is allocated and a pointer to it is returned by the function. size is the new size for the memory block, in bytes. If it is 0 and ptr points to an existing block of memory, the memory block pointed by ptr is deallocated and a NULL pointer is returned.
void free (void * ptr);
It frees the dynamically allocated memory which is created with either calloc() or malloc(). free function accepts void type pointer, which is pointer to allocated memory block by either malloc or calloc function.
Example : C Program to Read Dynamic array and Find Largest Value
printf("\n\t ENTER ARRAY SIZE:");
//Dynamic Memory Allocation i.e dynamic array
//Read Array Elements
printf("\n\t ENTER VALUE FOR %d element : ",i+1);
//Write Array Elements
printf("\n\t ARRAY ELEMENTS ARE: \n");
printf("\n\t Largest Number in Array is : %d",max);
DOWNLOAD CODE: dynamic_memory_allocation_malloc.c (3 downloads)