C – Dynamic Memory Allocation

Dynamic Memory Allocation:

  • Applications are used to store and process information.
  • We use variables to store the information.
  • We allocate memory to variables in 2 ways
    • Static memory allocation
    • Dynamic memory allocation

Using pointers, we can create variables which are ready to hold addresses of memory blocks. The size of memory block specified at runtime through Dynamic memory allocation.

stdlib.h: stdlib.h library contains pre-defined functions to allocate and de-allocate the memory at runtime.

  1. malloc(): allocates memory dynamically to structure variables
  2. calloc(): allocates memory dynamically to array variables
  3. realloc(): used to modify the size of Array created by calloc()
  4. free(): used to release the memory which is allocated.

Pointer type casting:

  • One type of pointer can points to same type of data.
  • Incompatible pointer type error occurs, when we try to access another type of data.
  • We can typecast from one type to another type as follows.
int main()
            int x=10;
            int* p1;
            char* p2;
            p1 = &x;
            printf(“x value is : %d \n”, *p1);
            p2 = (char*)p1;
            printf(“x value is : %d \n”, *p2);
            return 0;
Scroll to Top