Introduction to Pointers
A pointer in Pascal is a variable that holds the memory address of another variable, declared with the caret symbol, e.g. 'var p: ^Integer;' for a pointer to an integer, and dynamic memory is obtained with 'New(p)' and released with 'Dispose(p)'. Dereferencing — accessing the value a pointer refers to — uses the caret after the variable name, as in 'p^ := 42;', which is the reverse of how many other languages place the dereference operator before the variable.
Cricket analogy: A pointer is like a scorer's note that says 'see page 42 for Sachin's full stats' rather than writing out the entire career record in place — p^ is like actually flipping to page 42 to read the value.
Dynamic Data Structures
Pointers are the foundation for dynamic data structures in Pascal, most notably linked lists, where each node is a record containing data fields plus a 'next: ^TNode' field pointing to the following node, allowing the list to grow or shrink at runtime unlike a fixed-size array. Traversal walks the chain with a loop such as 'while current <> nil do begin ProcessNode(current); current := current^.next; end;', and the special value 'nil' marks the end of the list or an unassigned pointer.
Cricket analogy: A linked list is like a relay of fielding-position handoffs where each fielder just knows who to throw to next, rather than a fixed 11-slot array — the chain can effectively grow as substitutes rotate in during a match.
Common Pointer Pitfalls
Two classic pointer bugs in Pascal are dangling pointers, where 'Dispose(p)' frees the memory but p still holds the old (now invalid) address unless explicitly set to nil, and memory leaks, where 'New(p)' allocates memory that is never disposed because the last reference to it is overwritten or goes out of scope. Best practice is to set p := nil immediately after Dispose(p), and to ensure every New() has a matching Dispose() on every code path, including error-handling branches.
Cricket analogy: A dangling pointer is like a scoreboard operator who takes down Player 12's name display but forgets to clear the underlying data slot, so a stale name briefly reappears if that slot is queried again before being properly reset.
type
PNode = ^TNode;
TNode = record
data: Integer;
next: PNode;
end;
var
head, current, newNode: PNode;
begin
head := nil;
New(head);
head^.data := 10;
head^.next := nil;
New(newNode);
newNode^.data := 20;
newNode^.next := nil;
head^.next := newNode;
current := head;
while current <> nil do
begin
writeln(current^.data);
current := current^.next;
end;
end.Calling Dispose(p) frees the memory p points to, but p itself still holds that now-invalid address unless you explicitly assign 'p := nil' afterward — dereferencing a disposed pointer ('p^ := 5' after Dispose(p)) causes undefined behavior.
- A pointer holds a memory address; ^Type declares a pointer type and p^ dereferences it.
- New(p) allocates dynamic memory; Dispose(p) releases it back to the heap.
- Linked lists chain records together via a 'next: ^TNode' field, enabling runtime growth.
- 'nil' represents an unassigned pointer or the end of a linked structure.
- Dangling pointers occur when Dispose(p) is called but p is not reset to nil.
- Memory leaks occur when New(p) has no matching Dispose(p) on some code path.
- Always pair every New() with a corresponding Dispose(), including error branches.
Practice what you learned
1. How do you declare a pointer to an Integer in Pascal?
2. What does 'p^ := 42;' do, assuming p is a valid pointer?
3. What is a dangling pointer?
4. What field is typically used in a TNode record to build a linked list?
Was this page helpful?
You May Also Like
Records in Pascal
Understand how Pascal records bundle fields of different types into a single structured entity, including variant records.
Arrays in Pascal
Learn how Pascal's fixed-size, strongly-typed arrays work, from simple index ranges to multidimensional matrices.
Sets in Pascal
Explore Pascal's native set type — union, intersection, difference, and membership testing over ordinal values.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics