C gives you power over memory, and the responsibility that comes with it.
Direct answer
In C, local variables live on the stack and are cleaned up automatically. Memory from malloc lives on the heap and you must free it yourself. Common problems are leaks, buffer overflows, use after free and double free. Tools like Valgrind and AddressSanitizer find them.
Stack vs heap
| Stack | Heap | |
|---|---|---|
| Managed by | compiler | you |
| Lifetime | until the function returns | until you free it |
| Size | small, limited | large, flexible |
| Speed | very fast | slower |
Good habits
- Prefer the stack when size is known and small.
- Pair each allocation with a free.
- Check allocation results.
- Set pointers to
NULLafter freeing. - Run your program under a memory checker.
Try a sanitizer
gcc -g -fsanitize=address main.c -o main
./main
Where Teyro fits
Teyro's C for Beginners course turns this into short lessons you can play in a few minutes a day. It has 78 lessons, and you can start free.
The bottom line
Know who owns each piece of memory and when it ends.



