Back to series
· 10 min read

Why does memory have addresses?


I. Why Does Memory Need Addresses?

First, consider a question:

Suppose a computer has 16 GB of memory, which simultaneously holds the operating system, browser, editor, and data for various programs.

When the CPU wants to read a particular variable, how does it know exactly where that variable is stored?

Memory contains a large number of storage units. Without a positioning mechanism, the CPU cannot accurately find the data it needs.

Therefore, the computer assigns numbers to storage locations in memory. These numbers are memory addresses.

You can think of memory as a row of storage lockers:

  • Each locker has a number.
  • Each number corresponds to a storage location.
  • The corresponding location can be found through the number.
  • Reading and modifying data both require locating the appropriate position.

For example:

Memory Address Contents
0x1000 01000001
0x1001 01000010
0x1002 01000011
0x1003 00000001
0x1004 00000010

The CPU does not need to know the exact arrangement of all data throughout memory. It only needs the target address to issue the corresponding memory access request.

1. What Problem Do Addresses Solve?

Memory addresses mainly solve four problems.

First, locating data.

The CPU can use an address to find a specified storage location without searching all of memory from the beginning.

Second, distinguishing different data.

Different storage locations have different addresses, which prevents location confusion during access.

Third, supporting data reads and writes.

The CPU can read data according to an address and can also write calculation results to a specified location.

Fourth, supporting program organization.

Compilers, operating systems, and runtime environments can use addresses to manage variables, functions, objects, and data structures.

Therefore, memory addresses are the foundation for how computers organize and access data.


II. How Are Memory Addresses Generated?

To understand addresses, we first need to understand how computers organize memory.

1. Memory Consists of a Large Number of Storage Units

Computer memory is not a single data container; rather, it consists of a large number of storage units that can hold data.

In modern general-purpose computers, memory usually uses byte addressing.

That is, each byte has its own address.

A byte consists of 8 bits:

1 Byte = 8 Bits

For example, a contiguous section of memory can be abstracted as:

Address     Content
0x1000      01000001
0x1001      01000010
0x1002      01000011
0x1003      00000001
0x1004      00000010
0x1005      00000011

Here:

  • 0x1000 represents a memory address.
  • 01000001 represents one byte corresponding to that address.
  • 0x1001 represents the address of the next byte.

Addresses are arranged according to certain rules, enabling the CPU to locate different positions in memory.

Note: byte addressing is a common way of organizing modern computers, but not all computer architectures must use the same addressing unit.

2. Why Are Addresses Usually Contiguous?

Suppose a program needs to store a 4-byte integer.

If memory uses byte addressing, this integer usually needs to occupy 4 consecutive bytes.

For example:

Variable number, occupying 4 bytes

Address     Content
0x1000      00000000
0x1001      00000000
0x1002      00000000
0x1003      00001010

The variable starts at 0x1000 and occupies:

0x1000
0x1001
0x1002
0x1003

If the program needs to access the next byte, it can use 0x1004.

Contiguous addresses allow programs to conveniently organize arrays, structures, and other data structures.

For example:

int numbers[3] = {10, 20, 30};

Suppose int occupies 4 bytes and the array is stored contiguously:

numbers[0] → 0x1000
numbers[1] → 0x1004
numbers[2] → 0x1008

Therefore, array elements can be located using the starting address and the element offset.

3. Why Do Memory Addresses Use Hexadecimal?

A memory address is essentially a binary number.

However, if binary is used directly, the numbers become very long and inconvenient to read.

For example:

Binary:
00000000 00000000 00010000 00000000

Hexadecimal:
0x00001000

Each hexadecimal digit corresponds to 4 binary bits, making it easier to read and convert.

Common address representations:

Base Representation Example
Binary 0001000000000000
Decimal 4096
Hexadecimal 0x1000

In program development, debuggers, operating system logs, and memory analysis tools, addresses are usually displayed in hexadecimal.

For example:

#include <stdio.h>

int main() {
    int number = 100;

    printf("地址:%p\n", (void *)&number);

    return 0;
}

&number means obtaining the address of the variable number.

%p is used to output pointer addresses; its specific display format is implementation-defined, and a common form is hexadecimal.


III. How Does the CPU Access Memory Through Addresses?

With an address, the CPU can locate data.

However, when the CPU accesses memory, it does not simply take a number and go looking for a locker; it does so through a set of hardware and control mechanisms.

1. How Does the CPU Read Data?

Suppose the program needs to read the data at address 0x1000.

The basic process is as follows:

  1. The CPU executes an instruction that requires reading data.
  2. The CPU generates a memory access request.
  3. The request contains the target address.
  4. The processor checks the cache.
  5. If the cache hits, the data is retrieved from the cache.
  6. If the cache misses, it continues to access lower-level storage.
  7. If necessary, it accesses main memory through the memory controller.
  8. The data is returned to the CPU for use by subsequent instructions.
flowchart TD
    A["CPU executes read instruction"] --> B["Generate memory access address"]
    B --> C["Check CPU Cache"]
    C --> D{"Cache hit?"}
    D -->|Yes| E["Get data from Cache"]
    D -->|No| F["Request access to main memory"]
    F --> G["Memory controller handles request"]
    G --> H["Read data from DRAM"]
    H --> I["Return read result"]
    E --> J["CPU uses data"]
    I --> J

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#EAF3EC,stroke:#83A995,color:#315D49
    style C fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style D fill:#FFF3D9,stroke:#D6B66E,color:#705923
    style E fill:#DCEFE5,stroke:#4F8B70,color:#24563F
    style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style G fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style H fill:#EAF3EC,stroke:#83A995,color:#315D49
    style I fill:#DCEFE5,stroke:#4F8B70,color:#24563F
    style J fill:#DCEFE5,stroke:#4F8B70,color:#24563F

Here is one point that is easy to confuse:

Just because the CPU uses an address to access data does not mean every access reads DRAM directly.

In actual access, it may first hit the CPU Cache.

2. Address and Data Are Two Different Concepts

The address is used for locating, while the data is what is actually stored.

For example:

Address: 0x1000
Data: 00001010

It can be understood as follows:

  • The address answers: Where should I look?
  • The data answers: What is the content that was found?

When the CPU performs a read operation, it must provide an address, and the memory system returns the corresponding data.

When performing a write operation, the CPU provides the target address and the data to be written.

3. The Basic Process of Memory Reads and Writes

```mermaid flowchart TD A["CPU initiates a memory operation"] --> B{"Operation type"} B -->|Read| C["Provide the target address"] B -->|Write| D["Provide the target address and the data to write"] C --> E["Memory system locates the data"] E --> F["Return the data to the CPU"] D --> G["Locate the target storage location"] G --> H["Write the data"] F --> I["Read operation complete"] H --> J["Write operation complete"] style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976 style B fill:#FFF3D9,stroke:#D6B66E,color:#705923 style C fill:#EAF3EC,stroke:#83A995,color:#315D49 style D fill:#EAE8FA,stroke:#A49BD5,color:#51477D style E fill:#F6FAF7,stroke:#DCE9E0,color:#334155 style F fill:#DCEFE5,stroke:#4F8B70,color:#24563F style G fill:#F6FAF7,stroke:#DCE9E0,color:#334155 style H fill:#DCEFE5,stroke:#4F8B70,color:#24563F style I fill:#DCEFE5,stroke:#4F8B70,color:#24563F style J fill:#DCEFE5,stroke:#4F8B70,color:#24563F ```

Real hardware also involves mechanisms such as cache coherence, memory ordering, write buffers, bus transactions, and memory controllers.


IV. Why Don't Programs Use Physical Memory Addresses Directly?

If every program used physical memory addresses directly, many problems would arise.

Suppose the computer runs two programs at the same time:

  • Program A uses address 0x1000 to store a variable.
  • Program B also wants to use address 0x1000 to store a variable.

If both programs access physical addresses directly, they may conflict.

When Program A modifies data, it may affect Program B.

In addition, programs must also deal with:

  • Limited physical memory capacity.
  • Different programs needing different amounts of memory.
  • Programs possibly starting and ending at different times.
  • Physical memory needing to be allocated and reclaimed dynamically.
  • Isolation and protection between programs.

To solve these problems, modern operating systems introduced the virtual memory mechanism.

1. What Is a Virtual Address?

A virtual address is the address a program sees and uses while running.

A program usually does not need to know which physical memory location the data is actually stored in.

For example:

Program A:

virtual address 0x1000
       ↓
physical address 0x8000

Program B:

virtual address 0x1000
       ↓
physical address 0xA000

Both programs can use the virtual address 0x1000, but they map to different physical addresses.

This avoids conflicts caused by different processes directly using the same physical address.

2. What Is a Physical Address?

A physical address is an address used to locate an actual physical memory location.

For example:

virtual address: 0x1000
physical address: 0x8000

Programs use virtual addresses to access data, and the hardware and operating system are responsible for converting virtual addresses into physical addresses.

The two can be understood as:

Address type Meaning Main users
Virtual address The address seen by a process Applications, the CPU instruction execution environment
Physical address The actual memory location Memory management hardware, the operating system and platform

3. Why Can Virtual Addresses Repeat?

Each process has its own virtual address space.

For example:

Address space of Process A:

0x1000 → variable A
0x2000 → variable B

Address space of Process B:

0x1000 → variable X
0x2000 → variable Y

Although the address numbers are the same, they belong to different process address spaces.

The operating system distinguishes the mappings of different processes through page tables and the processor's address translation mechanism.

This means:

A virtual address must be combined with the address space context of the current process before its corresponding actual physical location can be determined.


V. How Is a Virtual Address Converted into a Physical Address?

Virtual address translation is one of the most important things to understand about memory addresses.

Modern operating systems usually use paging to manage memory, and the CPU completes address translation through the MMU (Memory Management Unit).

1. What Is the MMU?

The MMU is a hardware module in the processor, mainly responsible for:

  • Translating virtual addresses to physical addresses.
  • Checking memory access permissions.
  • Handling exceptions related to address mapping.
  • Supporting the operating system's virtual memory management.

When a program initiates a memory access, the CPU provides a virtual address, and the MMU determines the corresponding physical address based on the address translation information.

2. What Is a Page?

An operating system usually does not maintain a separate address mapping for every byte; instead, it divides memory into pages of a fixed size.

For example, suppose the page size is 4 KiB:

1 Page = 4 KiB = 4096 Bytes

The virtual address space can be divided into:

Virtual address space

Page 0
Page 1
Page 2
Page 3
Page 4

Physical memory is divided into frames of the same page size (Frame):

Physical memory

Frame 0
Frame 1
Frame 2
Frame 3
Frame 4

Virtual pages do not need to be mapped to contiguous physical frames.

For example:

Virtual Page 0 → Physical Frame 3
Virtual Page 1 → Physical Frame 1
Virtual Page 2 → Physical Frame 4
Virtual Page 3 → Physical Frame 0

This mapping method allows the operating system to allocate physical memory flexibly.

3. What Makes Up a Virtual Address?

Under paging, a virtual address can usually be divided into two parts:

  • Virtual page number (VPN): used to locate the virtual page.
  • Page offset (Offset): used to locate the specific position within the page.

For example:

Suppose the page size is 4 KiB:

4 \times 1024 = 4096 = 2^{12}

Therefore, the page offset requires 12 bits.

Suppose a simplified virtual address length is 32 bits:

Virtual address (32 bits)

┌────────────────────────────┬──────────────────┐
│   Virtual page number      │   Page offset    │
│          (VPN)             │    (Offset)      │
│         20 bits            │     12 bits      │
└────────────────────────────┴──────────────────┘

Where:

  • The high 20 bits represent the virtual page number.
  • The low 12 bits represent the page offset.

The MMU uses the virtual page number to look up the page table and find the corresponding physical frame number.

The page offset remains unchanged.

The physical address is formed by combining the physical frame number and the page offset.

4. Address Translation Example

Suppose:

  • The virtual address is 0x12345.
  • The page size is 4 KiB.
  • The physical frame number corresponding to the virtual page number is 0xA.

Since the page size is 4096 bytes:

Virtual page number = 0x12
Page offset = 0x345

The page table records:

Virtual page number 0x12 → Physical frame number 0xA

Therefore:

Physical address = Physical frame number + Page offset

Physical address = 0xA000 + 0x345

Physical address = 0xA345

Here, 0xA000 is the starting address of the physical frame.

The entire process can be represented as:

flowchart TD
    A["CPU generates virtual address 0x12345"] --> B["Split into virtual page number and page offset"]
    B --> C["Virtual page number 0x12"]
    B --> D["Page offset 0x345"]
    C --> E["Look up page table"]
    E --> F["Obtain physical frame number 0xA"]
    F --> G["Combine physical frame start address"]
    D --> H["Preserve page offset"]
    G --> I["Combine physical address"]
    H --> I
    I --> J["Physical address 0xA345"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style C fill:#DCEFE5,stroke:#4F8B70,color:#24563F
    style D fill:#DCEFE5,stroke:#4F8B70,color:#24563F
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#DCEFE5,stroke:#4F8B70,color:#24563F
    style G fill:#EAF3EC,stroke:#83A995,color:#315D49
    style H fill:#EAF3EC,stroke:#83A995,color:#315D49
    style I fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style J fill:#DCEFE5,stroke:#4F8B70,color:#24563F

This example uses a simplified single-level paging model to explain the principle. Real systems may use multi-level page tables, different page sizes, and more complex address translation structures.

5. What Is a Page Table?

A page table (Page Table) is a data structure used by the operating system to manage the mapping between virtual pages and physical frames.

For example:

Virtual page number Physical page frame number Valid
0 3 Yes
1 1 Yes
2 4 Yes
3 - No
4 0 Yes

When the CPU accesses a virtual address, the MMU needs to look up the page table using the virtual page number.

Besides recording physical page frame information, a page table entry usually also contains:

  • The valid bit.
  • Read and write permissions.
  • User-mode and kernel-mode access permissions.
  • Whether execution is allowed.
  • Access status.
  • Dirty page status.

This information is used not only for address translation, but also for memory protection.

6. What Is a Multi-Level Page Table?

If a process has a very large virtual address space, a single-level page table may take up a large amount of memory.

A multi-level page table manages page tables through hierarchical indexing.

For example:

Virtual address
    ↓
Level-1 page table index
    ↓
Level-2 page table index
    ↓
Level-3 page table index
    ↓
Offset within page

The CPU walks the page tables level by level according to the different parts of the virtual address, and finally obtains the physical page frame number.

A multi-level page table avoids having to allocate a complete page table structure for entire portions of the address space that are never used.

7. What Is the TLB?

If every memory access required walking a multi-level page table, it would incur extra overhead.

Therefore, processors use the TLB (Translation Lookaside Buffer) to store recently used address mappings.

When the CPU accesses a virtual address:

  1. It first looks up the TLB.
  2. If it hits, the physical page frame information is obtained directly.
  3. If it misses, the page table must be queried.
  4. Once the mapping is obtained, the TLB is usually updated.
  5. The access to the corresponding physical memory then continues.
flowchart TD
    A["CPU issues a virtual address"] --> B["Look up the TLB"]
    B --> C{"TLB hit?"}
    C -->|Yes| D["Obtain the physical page frame number directly"]
    C -->|No| E["Walk the multi-level page table"]
    E --> F{"Is the page table mapping valid?"}
    F -->|Yes| G["Obtain the physical page frame number"]
    F -->|No| H["Trigger a page fault or access exception"]
    G --> I["Update the TLB"]
    I --> J["Compose the physical address"]
    D --> J
    J --> K["Access physical memory"]
    H --> L["Operating system handles the exception"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#EAE8FA,stroke:#A49BD5,color:#51477D
    style C fill:#FFF3D9,stroke:#D6B66E,color:#705923
    style D fill:#DCEFE5,stroke:#4F8B70,color:#24563F
    style E fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style F fill:#FFF3D9,stroke:#D6B66E,color:#705923
    style G fill:#DCEFE5,stroke:#4F8B70,color:#24563F
    style H fill:#F7EAE7,stroke:#C98D7C,color:#754438
    style I fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style J fill:#EAF3EC,stroke:#83A995,color:#315D49
    style K fill:#DCEFE5,stroke:#4F8B70,color:#24563F
    style L fill:#F7EAE7,stroke:#C98D7C,color:#754438

The TLB reduces the overhead of address translation and is an important part of memory access performance in modern processors.


VI. Why Does the Same Variable Have Its Own Memory Address?

Now that we understand addresses and virtual memory, let us return to variables in programming languages.

A variable is not just a name.

While a program is running, a variable needs to be associated with some kind of runtime storage location, or the compiler may optimize it away using registers and other means.

1. A Variable Name and a Memory Address Are Not the Same Thing

For example:

int age = 25;

Here:

  • age is the variable name.
  • 25 is the variable's value.
  • &age can be used to get the variable's address.

The variable name is mainly an identifier used while writing and compiling the program.

The memory address is used at runtime to locate the actual storage location.

You can observe this with the following code:

#include <stdio.h>

int main() {
    int age = 25;

    printf("变量值:%d\n", age);
    printf("变量地址:%p\n", (void *)&age);

    return 0;
}

Possible output:

Variable value: 25
Variable address: 0x7ffe12345678

The exact address may differ each time the program runs.

Reasons include:

  • The address space allocated by the operating system to the process may differ.
  • The location of the stack may change.
  • Address space layout randomization (ASLR) changes the location of some regions.
  • The compiler may use different code generation and optimization strategies.

2. Why is the address-of operator & needed?

In C, & is the address-of operator.

For example:

int number = 100;

int *p = &number;

Here:

  • number stores the integer value 100.
  • &number obtains the address of the variable.
  • p stores that address.
  • *p means accessing the object pointed to by the pointer.

This can be understood as:

number
  │
  ├── Value: 100
  │
  └── Address: 0x1000

p
  │
  └── Stores address: 0x1000

3. Why do pointers need to store addresses?

A pointer is a variable that stores a memory address.

For example: