Before diving into pointers, understand the two main memory regions C programs use: the stack and the heap.
Stack (automatic storage)
Local variables inside a function live on the stack. They are created when the function is called and destroyed when it returns—no free needed.
void greet(void) {
int count = 1; /* stack: gone when greet returns */
}
Heap (dynamic storage)
malloc requests memory that stays alive until you call free. Use it when size or lifetime exceeds a single function call.
Addresses and pointers (preview)
Every object has an address. A pointer stores that address: int *p = &count;. Dereferencing *p reads or writes through the pointer.
Important interview questions and answers
- Q: Stack vs heap?
A: Stack is automatic and fast; heap is manual, flexible, and must be freed to avoid leaks. - Q: What is a dangling pointer?
A: A pointer to memory that is no longer valid—often afterfreeor returning a pointer to a local stack variable.
Self-check
- Where do local non-static variables live?
- What function releases heap memory?
Tip: Draw boxes and arrows on paper for stack vs heap—visualizing addresses makes pointer lessons in Rust and C much easier later.
Interview prep
- Stack vs heap?
Stack: automatic, fast, limited, tied to function calls. Heap: manual
malloc/free, flexible lifetime, risk of leaks and fragmentation.- What is a dangling pointer?
A pointer referencing memory that is no longer valid—common after
freeor returning address of a local variable.