Memory allocation is the process by which a program reserves a portion of a computer's RAM for its variables, data structures, and execution context. Allocation can be static (at compile time), automatic (on the call stack), or dynamic (on the heap at runtime using functions like malloc in C or new in C++/Java). Proper memory management is critical for performance and preventing bugs such as memory leaks, buffer overflows, and dangling pointers.
| Type | Location | Lifetime | Managed By | Example |
|---|---|---|---|---|
| Static | Data segment | Program lifetime | Compiler | Global variables in C |
| Stack (automatic) | Stack | Function scope | CPU/OS | Local variables |
| Heap (dynamic) | Heap | Until freed | Programmer / GC | malloc(), new |
| Memory-mapped | Virtual memory | Explicit unmap | OS | mmap() in POSIX |
| Register | CPU registers | Instruction scope | Compiler | Loop counter optimisation |
Wikimedia Commons, CC BY-SA
A pointer is a variable that stores the memory address of another variable rather than a data value directly. Pointers enable direct memory access and manipulation, dynamic memory allocation, and efficient passing of large data structures without copying. They are central to languages such as C and C++, and understanding them is key to systems programming, data structure implementation, and performance-critical software.
Garbage collection (GC) is an automatic memory management process that reclaims heap memory occupied by objects no longer reachable or needed by a program. By tracking object references and periodically freeing unreachable memory, GC eliminates common bugs such as memory leaks and dangling pointers without requiring explicit deallocation by the programmer. It is a core feature of languages including Java, Python, Go, C#, and JavaScript, with algorithms such as mark-and-sweep, reference counting, and generational collection.
From Latin "memoria" (memory) and "allocare" (to assign/place). The concept of dynamic memory allocation was formalised with the development of heap management in LISP (1960) and later standardised in C (1970s).