Memory & Pointers

an address and the address space

If memory is a street of numbered boxes, an address is simply the number on one box — it tells you where a byte lives, not what is in it. The whole range of valid numbers, from the first box to the last, is the address space: the complete set of addresses a program is allowed to talk about.

An address is itself a plain integer, usually written in hexadecimal because it lines up with bytes nicely, like 0x7fffe3a04c10. On a 64-bit machine an address is 64 bits wide, so in principle the address space runs from 0x0 up to a 16-exabyte ceiling, though a real program only ever uses a tiny scattered fraction of that. When a C program runs, its addresses are virtual: the operating system gives each process its own private numbering that starts fresh, so two programs can both 'use address 0x1000' without colliding.

Addresses matter because a pointer is nothing more than a stored address. To pass around, compare, or save a location, you save its number. The reason 0 is special — the null pointer — is just a convention: address 0 is reserved to mean 'points to nothing', and the operating system deliberately leaves it unmapped so a stray access there crashes loudly rather than corrupting real data.

int x = 5; printf("%p\n", (void*)&x); might print 0x7ffd1c2a4b6c — that number is the address of x, the box where its 4 bytes begin, not the value 5.

&x is the box number; the value 5 lives inside that box.

The addresses a C program sees are virtual addresses chosen by the OS, not raw physical chip locations; how virtual maps to physical (paging) is covered later in Field l. The point here is only that an address is a number naming a byte.

Also called
memory address記憶體位址