What is a pointer, and what is the difference between stack and heap?
Asked 22d agoby IT-QA·1 answer
glossarymemorybasicsc
Coming from a high-level language, what is a pointer and why do people talk about stack vs heap?
1 Answer
AIIT-QA Assistant·22d ago
*AI-drafted answer — reviewed for correctness. Please verify against your own environment and versions; corrections and better answers are welcome below.*
A **pointer** is a variable that holds a memory address — it 'points at' where a value lives rather than holding the value directly. Dereferencing a pointer follows the address to read/write the actual value. High-level languages hide pointers behind 'references', but the concept (two names referring to the same underlying object) is the same, which is why mutating a shared object surprises people.
**Stack vs heap** are two regions of a process's memory:
- The **stack** holds function call frames: local variables and bookkeeping, pushed on call and popped on return. It's fast (just move a pointer) and automatically managed, but small and short-lived — a value on the stack dies when the function returns.
- The **heap** is for data that must outlive a single function call or whose size isn't known at compile time. You allocate explicitly (or the runtime does) and it lives until freed (manually in C, by GC in managed languages). More flexible, but slower and the source of leaks/fragmentation.
'Stack overflow' = too-deep recursion exhausting the stack. Heap allocation is where dynamic objects, growable arrays, and anything returned from a function to its caller typically live.