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.
| Operation | Syntax (C) | Meaning | Example Result |
|---|---|---|---|
| Declaration | int *p; | p is a pointer to int | p holds an address |
| Address-of | p = &x; | Assign address of x to p | p = 0x7fff5a |
| Dereference | *p | Value at address p | Value of x |
| Pointer arithmetic | p + 1 | Next int address (+4 bytes) | Next array element |
| NULL pointer | p = NULL; | Pointer points to nothing | Safe uninitialised state |
Wikimedia Commons, CC BY-SA
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.
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 Old French "pointer" (to indicate or point). In computing, the term was popularised during the development of languages like BCPL and C (Dennis Ritchie, early 1970s) to describe variables that "point to" memory locations.