Where is Memory Allocated?
When you allocate memory dynamically in C, it is allocated in a region of memory known as the heap. The heap is a pool of memory used for dynamic memory allocation, distinct from the stack, which is used for static memory allocation and function call management.
Key characteristics of the heap:
- Dynamic Allocation: Memory is allocated and deallocated manually using specific functions.
- Flexible Size: You can allocate memory blocks of varying sizes during runtime.
- Persistent: Allocated memory remains until it is explicitly deallocated using the appropriate function.
Memory Segments in C Program
Segment | Purpose | Example Variable Type |
---|---|---|
Text | Program instructions (code) | — |
Data | Global/static variables (initialized) | static int score = 10; |
BSS | Global/static variables (uninitialized or zero-initialized) | static int count; |
Stack | Local variables, function calls | int total = 5; |
Heap | Dynamically allocated memory (via malloc, etc.) | int *ptr = malloc(4); |
Functions for Dynamic Memory Allocation
C provides several standard library functions for dynamic memory allocation, all of which are declared in the <stdlib.h> header file:
1. malloc()
Allocates a specified number of bytes of uninitialized memory.
int *ptr = (int *)malloc(sizeof(int) * 5);
Note: The allocated memory contains garbage values; it should be initialized before use.
2. calloc()
Allocates memory for an array of elements and initializes all bytes to zero.
int *ptr = (int *)calloc(5, sizeof(int));
Note: Useful when you need zero-initialized memory.
3. realloc()
Resizes previously allocated memory to a new size.
ptr = (int *)realloc(ptr, sizeof(int) * 10);
Note: Can be used to expand or shrink the size of the allocated memory block.
4. free()
Deallocates previously allocated memory, returning it to the heap.
free(ptr);
Note: Always free dynamically allocated memory to prevent memory leaks.
Understanding the Heap
The heap is a special part of a program’s memory used to dynamically allocate space while the program is running. Unlike the stack, which follows a simple last-in, first-out rule, the heap gives you more control and flexibility in how memory is used and released.
Key Features:
- Programmer Managed: You choose when to allocate and free memory.
- Risk of Fragmentation: Poor memory handling can break the free space into small pieces, making it harder to use.
- Slower than Stack: Because the system has to track memory usage carefully, heap operations are usually a bit slower.
Example
#include <stdio.h> #include <stdlib.h> int main() { int *quiz = (int *)malloc(3 * sizeof(int)); // Allocated on Heap if (quiz == NULL) { printf("Memory allocation failed\n"); return 1; } quiz[0] = 10; quiz[1] = 20; quiz[2] = 30; printf("Quiz Scores: %d, %d, %d\n", quiz[0], quiz[1], quiz[2]); free(quiz); // Releases memory back to heap's free pool return 0; }
Output:
Quiz Scores: 10, 20, 30
This C program uses malloc to allocate memory for three quiz scores on the heap. It checks if memory allocation was successful, then stores and prints the scores. After use, it frees the memory with free() to avoid memory leaks. This shows basic dynamic memory handling in C.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Check C Books
- Apply for C Internship
- Check Computer Science Books