How Arrays Are Stored in Memory?
Let’s understand the memory concept of arrays step by step, in easy language, with a proper example.
What happens in memory when an array is created?
When you declare an array, the computer:
- Reserves a block of memory
- Stores all elements next to each other
- Does not leave gaps between elements
This is called continuous memory allocation.
Example Array
int num[5] = {2, 8, 7, 6, 0};Assume:
inttakes 4 bytes- Base address (starting address) = 2000
How Elements Are Stored in Memory
| Array Element | Index | Value | Memory Address |
| num[0] | 0 | 2 | 2000 |
| num[1] | 1 | 8 | 2004 |
| num[2] | 2 | 7 | 2008 |
| num[3] | 3 | 6 | 2012 |
| num[4] | 4 | 0 | 2016 |
Explanation:
- First element starts at base address
- Each next element is stored after 4 bytes
- Addresses increase regularly
How Address Is Calculated?
The computer uses this formula:
Address of num[i] =
Base Address + (i × size of data type)Example:
Address of num[3] =
2000 + (3 × 4)
= 2012Why Arrays Are Fast?
Because memory is continuous:
- CPU can directly calculate the address
- No searching is required
- Any element can be accessed quickly using its index
This is why:
num[3]is accessed very fast.
Memory Depends on Data Type
Different data types use different memory sizes.
char ch[5]; // 1 byte each
int a[5]; // 4 bytes each
float f[5]; // 4 bytes eachSo memory spacing depends on the data type, not the value.
Important Points for Students
- Array elements are stored in continuous memory
- Index starts from 0, not from memory address
- Size of array = number of elements × size of data type
- Array size is fixed at declaration time
Simple Summary
When an array is created, the system allocates a continuous block of memory. Each element is placed one after another, and the address of any element can be calculated easily using its index. This makes arrays fast and efficient.
