Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Memory Management in C Programming

C programming has three types of memory allocation Static, Automatic & Dynamic.

Static Allocation - It is allocated when you declare a static or global variable. The memory space is allocated once when the program starts and is freed when program ends.

Automatic Allocation - It is allocated when you declare normal varible like function argument, or a local variable. This memory is allocated when any function is called or any block of code is executed.

Dynamic Allocation - In C programming library functions are used to allocate/deallocate a block of memory on the heap (part of main memory) dynamically. The C code access the block of allocated memory via a pointer returns by functions like malloc(). When memory is no longer needed, the pointer is passed to free() for releasing of memory os that the freed memory can be used by other programms running on computer.

C Library functions used in Memory Management

  • malloc()
  • free()
  • calloc()
  • realloc()

malloc()

free()

calloc()

ealloc()

Dynamic Memory is allocated on Heap

Heap is a chunk of memory that users can use to dynamically allocate memory. If users fails to release/deallocate the memory then it lasts until program exists, this is also known as memory leak in the code.