Introduction
Before diving into data structures, it helps to understand how computers store data at a low level. Computers process data in binary form, represented by bits (0s and 1s). These bits are grouped into bytes, and bytes represent various types of data such as integers, characters, and more complex structures. As a developer, you work with variables in your code. These variables are stored in the computer’s memory so they can be accessed and manipulated during program execution — this is where RAM comes in.What is RAM?
RAM stands for Random Access Memory — a type of computer memory used to store data that is actively being used or processed. RAM is volatile memory, meaning it loses its contents when the computer is turned off.1 byte = 8 bits. A bit is a
0 or 1. RAM capacity is measured in bytes (KB, MB, GB).| RAM Size | Bytes | Integers (4 bytes each) |
|---|---|---|
| 8 GB | 8,589,934,592 | ~2.1 billion |
| 16 GB | 17,179,869,184 | ~4.3 billion |
Components of RAM
RAM can be thought of as a contiguous block of data where each location has two components:- Value — the actual data being stored (e.g., an integer, a character).
- Address — the location where the value is stored in memory, used to access the value.
What is a contiguous block of data?
Data is stored in a continuous sequence of memory addresses with no gaps. If an array starts at address
1000 and each element takes 4 bytes, the second element is at 1004, the third at 1008, and so on. This enables fast O(1) index access by computing address = base + (index × element_size).Binary Representation
Computers use binary to store and process all data. Each digit in a binary number is a bit. Multiple bits are grouped to form larger data types:| Unit | Bits | Example |
|---|---|---|
| Bit | 1 | 1 |
| Byte | 8 | 00000101 |
| Word | 16 | 00000000 00000101 |
| Double Word | 32 | 00000000 00000000 00000000 00000101 |
Binary numbers are based on powers of 2. Each position represents a power of 2, starting from 2⁰ on the right.To convert binary to decimal: Add up the values where the bit is
1.5 in binary:
- 8-bit:
00000101 - 32-bit:
00000000 00000000 00000000 00000101
| Decimal | 8-bit | 16-bit | 32-bit |
|---|---|---|---|
| 1 | 00000001 | 00000000 00000001 | ... 00000001 |
| 2 | 00000010 | 00000000 00000010 | ... 00000010 |
| 3 | 00000011 | 00000000 00000011 | ... 00000011 |
| 4 | 00000100 | 00000000 00000100 | ... 00000100 |
| 5 | 00000101 | 00000000 00000101 | ... 00000101 |
| 8 | 00001000 | 00000000 00001000 | ... 00001000 |
| 10 | 00001010 | 00000000 00001010 | ... 00001010 |
How Data is Stored in RAM
- Array of Integers
- Array of Characters
Consider the array Elements in the array are stored continuously and in order. You cannot choose which memory location the data is placed in — the OS and runtime handle that.
[1, 2, 3]. Each integer is converted to binary and stored in contiguous memory addresses.Why does the address increment by 4?
Each integer takes 4 bytes (32 bits) of memory. Since each memory address points to 1 byte, you need 4 consecutive addresses per integer.Address formula:
Element address = base_address + (index × element_size)Array[0] = 100 + (0 × 4) = 100Array[1] = 100 + (1 × 4) = 104Array[2] = 100 + (2 × 4) = 108