Memory: The Stack, the Heap, and Pointers
Where your variables actually live, why the heap costs more than the stack, and what a pointer really is.
It's easy to picture memory as one long row of identical bytes.
In practice there are several kinds of storage. The fast kinds are small, and the big kinds are slow. The memory your program can use is also split into areas that are used in different ways. Knowing this layout explains pointers, stack overflows, and how Go decides where to put your variables.
The memory hierarchy
The CPU (Central Processing Unit) needs data quickly, but large storage is slower than small storage. So computers use several layers of storage. Each layer is roughly ten times faster, and much smaller, than the layer below it.
| Level | Typical size | Typical latency |
|---|---|---|
| Registers | a few hundred bytes | under 1 cycle, about 0.3 ns |
| L1 cache | 32 to 64 KB | about 1 ns |
| L2 cache | 256 KB to 1 MB | about 4 ns |
| L3 cache | 8 to 32 MB | about 15 ns |
| RAM | 8 to 128 GB | about 100 ns |
| SSD | 0.5 to 4 TB | about 100,000 ns |
Registers are the few storage slots inside the CPU. A cache is a small, fast memory on the processor chip that keeps copies of recently used data. RAM (Random Access Memory) is the computer's main memory, and an SSD (Solid-State Drive) is where files are stored. The last step is the biggest: an SSD is roughly a thousand times slower than RAM.
Fast software keeps the data the CPU needs in the top few rows. The caches help automatically. When you read one byte, the cache loads the 64 bytes around it, which is called a cache line. So when your next read is the byte right after, it's already in the cache. That's why reading memory in order is much faster than jumping to random places by following pointers.
Inside your process: stack versus heap
A running program is called a process. When it starts, the operating system gives it its own range of memory addresses. Two areas in that range matter most.
The stack grows and shrinks automatically as functions are called and return. It's fast, it's used in strict order, and it has a fixed limit, usually between 1 and 8 MB.
The heap is for data that has to stay alive after the function that created it returns, or whose size isn't known until the program runs. It's flexible. In C you free heap memory yourself. In Go a garbage collector finds memory that is no longer used and frees it for you.
Stack frames
Every function call adds a stack frame to the stack. A frame is a block of memory that holds the call's local variables, its arguments, and the return address, which is where to continue when the function finishes.
When the function returns, its frame is removed at once. The CPU only moves a register called the stack pointer back up. There is no searching for free space and no record keeping. Making room on the stack is one subtraction, and freeing it is one addition.
That's why stack allocation costs almost nothing. It's also why a function that calls itself forever causes a stack overflow. Frames keep being added until the stack's limit is reached.
What a pointer actually is
A pointer is a variable whose value is a memory address. An address is a number that names one location in memory. On a 64-bit machine, a pointer is eight bytes holding something like "the data is at byte 0x7ffd".
x := 42
p := &x // p holds the address of x
fmt.Println(*p) // dereference: go to that address, read → 42
*p = 7 // go to that address, write
fmt.Println(x) // → 7
The ampersand, &, means "give me the address of this". The asterisk, *, means "go to that address and use what is there". These are the only two things you do with pointers.
Everything built from pointers, such as linked lists, trees, slices and methods with pointer receivers, comes down to storing addresses and following them.
Why heap allocation costs more
On the stack, making room means moving one register. On the heap, the allocator, the code that hands out heap memory, has to find a free block that is big enough and mark it as used. In Go, the garbage collector later has to check whether anything still points at that block before it can free it.
The stack never does any of that work. Objects on the heap also tend to be spread out in memory. Following pointers between them often misses the cache, and each miss costs about 100 ns to reach RAM.
Stack allocation costs almost nothing and cleans up after itself. Heap allocation costs time twice, once to find space and again to free it later, and it often makes reads slower by missing the cache. Use the stack when you can.
Escape analysis: who decides?
In Go you never ask for heap memory directly. The compiler decides between the stack and the heap, using a check called escape analysis.
If the compiler can prove that a value isn't used after its function returns, the value goes on the stack. If a reference to it escapes, the value has to go on the heap. A reference escapes when it's returned, stored in a global variable, or captured by a function that lives on after the call.
func stackAlloc() int {
v := 10 // never escapes → stays on the stack
return v // the value is copied out
}
func heapAlloc() *int {
v := 10 // its address escapes via the return
return &v // must live on the heap
}
You can ask the compiler to show its decisions.
$ go build -gcflags='-m' main.go
./main.go: v doesn't escape
./main.go: moved to heap: v
In stackAlloc, the value stays on the stack and is freed for free when the function returns. In heapAlloc, we return the value's address, so the value has to stay alive after the frame is gone. Go puts it on the heap, and from then on the garbage collector decides when to free it.
The two functions look almost the same, but their costs are very different, and one compiler flag tells you which is which.
The Bottom Line
I write Go tools on Arch, and most questions that start with "why is this allocating" are answered by this section.
If a busy loop returns the address of something on every pass, each pass gives the garbage collector more work. If you pass values instead, reuse buffers, or keep values from escaping, the memory moves onto the stack, where it costs almost nothing.
Registers, then cache, then RAM, then disk. The stack is cheap, and the heap costs more. With those two facts, you can reason about any decision about memory or pointers.
Check yourself
3 questions · nothing is savedReferences & Primary Sources
Official specifications, standards, and documentation cited in this post.
- [1]
Defines the x86-64 execution stack frame conventions, register allocation, and call stack layout.
SPECgitlab.com - [2]
Standard specification for dynamic memory allocation, alignment, and process heap management.
STANDARDpubs.opengroup.org
- Written
- Oct 22, 2025
- Length
- 1,178 words · 5 min
- Built
- …
- Served by
- …
- Delivered
- …
- Over the wire
- …
- Go's share
- …
USB Sticks, Disks, and the Mysterious 4GB Wall
Why a 64GB USB stick refuses a 5GB file, what a file system actually is, and how FAT32, exFAT and NTFS really differ inside.
The Two Jobs of the Asterisk: Pointers in Go and C++
Pointers are often confusing, and much of the reason is one detail: the asterisk means two different things depending on where it is. Here's how to read it, in Go and in C++.
From C++ to Go: Demystifying Pointers, Slices, and Errors
Go code looks like C++, but the ideas behind it are different. Three things I had to learn again while building a todo app in Go: errors are values, a slice is a small header that points at the data, and the asterisk does two jobs.
List* l or List *l? The Oldest Style War in Programming
Three spellings that mean the same thing to the compiler. People have argued about where the asterisk goes since the 1970s, because the question is really about what a declaration is. Here's the whole debate, and how Go avoided it.
Projects built with what this post is about.


