Chapter 35 of 37

Memory Management in C

Every C program needs memory to store variables, arrays, functions, and other data while it runs.

Memory management is about understanding how this memory is used and, especially, how dynamically allocated memory is handled.

What is Memory Management?

Memory management is the process of allocating, using, and releasing memory during program execution.

C gives programmers a high level of control over memory, which is one of its biggest strengths.


Main Areas of Memory

A running C program typically uses different memory areas for different purposes:

Memory Area

Common Use

Stack

Local variables and function call information

Heap

Dynamically allocated memory

Static/Global Area

Global and static variables

Code/Text

Program instructions

For example:

int global = 10;

int main()
{
    int local = 20;

    int *ptr = malloc(sizeof(int));
}

Here:

  • global → static/global storage

  • local → typically stored on the stack

  • Memory returned by malloc() → allocated on the heap


Stack Memory

The stack is commonly used for local variables and function-call information.

void test()
{
    int age = 20;
}

The local variable age has automatic storage duration and normally exists while the function's block is executing.

The programmer generally doesn't need to manually free this memory.


Heap Memory

The heap is used for dynamic memory allocation.

For example:

int *ptr = malloc(sizeof(int));

The memory is allocated while the program is running.

When it is no longer needed, we should release it:

free(ptr);

If dynamically allocated memory is not released when appropriate, it can cause a memory leak.


Dynamic Memory Management

C provides four important functions through <stdlib.h>:

malloc()   → Allocate memory
calloc()   → Allocate and initialize memory to zero
realloc()  → Resize allocated memory
free()     → Release memory

Example:

int *numbers = malloc(5 * sizeof(int));

if (numbers != NULL)
{
    // Use the memory

    free(numbers);
}

Always check whether allocation succeeded before using the returned pointer.


Memory Leak

A memory leak occurs when dynamically allocated memory is no longer needed but is not released.

For example:

int *ptr = malloc(sizeof(int));

/* ptr is lost without calling free() */

If this happens repeatedly, especially in a long-running program, the program can consume more and more memory.


Dangling Pointer

A dangling pointer is a pointer that refers to memory that has already been released or is no longer valid.

For example:

int *ptr = malloc(sizeof(int));

free(ptr);

After free(ptr), using *ptr is invalid.

A common practice is:

free(ptr);
ptr = NULL;

This doesn't restore the memory, but it helps avoid accidentally using the old address through that pointer.

In Simple Words

Memory management in C means properly allocating, using, and releasing memory.

The most important things to remember are:

Stack → Automatic/local memory

Heap → Dynamically allocated memory

malloc() / calloc() → Allocate

realloc() → Resize

free() → Release

Good memory management is important because incorrect memory handling can lead to memory leaks, dangling pointers, buffer overflows, and program crashes.