Memory Allocation

---------------------

1. Below  are widely used dynamic memory allocation library calls (and not system calls)

  a.  malloc()

  b.  calloc() - This function is same as malloc() but initializes the memory allocated with '0'

  c.   realloc() - Is used if there is a need to change the size of already allocated memory.

2. Dynamic memory is allocated in 'Heap' segment

3.  All above functions return (void*) - pointer to allocated memory if success, NULL if failure

4.  syntax:

#include <stdlib.h>

void *malloc(size_t size);

void *calloc(size_t nmemb, size_t size);

void *realloc(void *ptr, size_t size);


5. free() is used for freeing the dynamically allocated memory

    Syntax of free():      void free(void *ptr); // ptr variable points to the memory that needs to be freed.

6. Do not  call free() on same pointer (same memory location) twice.

7. After free(), it is good practice to assign the pointer to NULL(because the memory is already de-allocated, and the pointer now does not point to a valid memory location)