100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Pointers in Pascal

Understand Pascal pointers, dynamic memory allocation with New/Dispose, and how they power dynamic structures like linked lists.

Data StructuresAdvanced10 min readJul 10, 2026
Analogies

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.

pascal
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

Was this page helpful?

Topics covered

#Programming#PascalStudyNotes#PointersInPascal#Pointers#Pascal#Dynamic#Data#StudyNotes#SkillVeris#ExamPrep