Back to Module Topics

2. Dynamic Memory

Dynamic Memory Allocation

Dynamic memory allocation evaluates exclusively utilizing the <stdlib.h> library natively during programmatic application Runtime.

Static vs. Dynamic Allocation

ParameterStatic AllocationDynamic Allocation
SpacePre-calculated size exclusively correctly compiled.Evaluated totally dynamically via runtime requests.
ArchitectureMemory bound to the Stack.Memory allocated to the Heap flexibly.
PointersPointers exclusively specifically used for referenced data.Required inherently to access the allocated Heap spaces.
Exampleint arr[100];int *p = malloc(100 * sizeof(int));

Memory Segments (Stack vs. Heap)

  1. Stack Memory: Controlled automatically by the OS.
  2. Heap Memory: Developer manually controls dynamically.

Operations (malloc, calloc, realloc, free)

1. malloc() (Memory Allocation)

malloc() fundamentally allocates a specified block of memory, leaving it uninitialized.

int *ptr = (int*) malloc(5 * sizeof(int)); // Explicitly allocates dynamically 

2. calloc() (Contiguous Allocation)

calloc() contiguously allocates sequential block spaces inside memory, initializing them to zero.

int *ptr = (int*) calloc(5, sizeof(int)); // Initializes safely the block values

3. realloc() (Re-Allocation)

realloc() dynamically expands or shrinks natively predefined bound memory blocks.

ptr = (int*) realloc(ptr, 10 * sizeof(int));

4. free()

free() safely returns allocated memory to the OS.

free(ptr); // Prevents memory leaks.
Cimp - C Programming Guide