Memory Management functions
Memory Allocation Casting: Prior to C99, it was necessary to cast the pointer that returned from a memory allocation function. Now it is no longer necessary, as it does no harm as long as the cast is correct.
If you should be working with an earlier standard, the casting format is: pointer = (type*) malloc(size)
Block Memory Allocation – Malloc
malloc allocated the required memory and returns a pointer to the allocated memory. It returns a null pointer if memory is not available.
Contiguous Memory Allocation – Calloc
calloc is used for allocating memory for dynamic arrays. It sets the allocated bytes to null characters
Reallocation of Memory – Realloc
void * realloac(void *ptr, size_t, new size): changes the size of the previously allocated memory block either by deleting or extending the memory at the end of the block.
Releasing Memory (free)
void free(void *ptr): releases the memory (back to heap) pointed by the pointer. The value of the pointer does not change (it still points to the address in heap). It is recommended to set the pointer to null after the free operation.
Using a pointer after its memory has been released is a common programming error. Guard against it by clearing the pointer.
Social Plugin