C++ PROGRAMMING · 10 MIN STUDY

Pointers and memory addresses

A pointer stores an address. Dereferencing a valid pointer accesses the object located at that address.

Learning objectives

  • Take an address with & and declare a pointer with *.
  • Read and write an object through a valid pointer.
  • Explain null, dangling, and uninitialized pointers.
  • Compare a pointer with a reference at a beginner-appropriate level.

Prerequisites: variables, address-of syntax, functions, and object lifetime at a basic level. If memory addresses are unfamiliar, first review variables and scope.

Why pointers exist

Programs sometimes need to refer to an object indirectly: an optional object may be absent, a data structure may link nodes together, or a function may work with an object supplied elsewhere. A pointer represents that connection by holding an address. An address is a location in a program’s memory model; treat it as an implementation detail you can use through the language rules, not as a meaningful human number.

value: 42 address: &value ──→ [ int object containing 42 ] pointer: address ──→ same object

Declare, point, dereference

C++
int value = 42;
int* pointer = &value;

std::cout << *pointer << '\n';
*pointer = 99;
std::cout << value << '\n';

&value obtains the address of value. The type int* means “pointer to an int.” In an expression, *pointer dereferences it: it accesses the int at the stored address. The assignment through the pointer changes the original object, so the output is 42 followed by 99.

Safety rules

  • Initialize a pointer. A pointer with an indeterminate value must not be dereferenced.
  • Use nullptr when a pointer intentionally points to no object, and check it before dereferencing.
  • A pointer becomes dangling if the object it referred to no longer exists. Dereferencing one has undefined behavior.
  • Pointer arithmetic is only defined within the bounds of the same array object (or one past it, which may not be dereferenced). It is not a general way to wander through memory.

Pointers versus references

A reference is another name for an existing object after it is initialized. A pointer is an object that stores an address and can be null or reassigned to a different object. Both can provide indirect access, but they communicate different intent. Choose the simplest form that accurately models the possibility of absence and ownership.

Practical applications

Linked lists, trees, polymorphic interfaces, and resource-management utilities use indirection. In modern C++, prefer RAII types such as std::vector, std::unique_ptr, and std::shared_ptr when they match ownership; raw new and delete make leaks and dangling pointers easier to create.

Common mistakes

  • Writing int* p; and immediately using *p.
  • Returning a pointer or reference to a local variable that has gone out of scope.
  • Using delete on memory not obtained with a matching new.

Summary

Pointer syntax is compact, but its responsibility is real: dereference only a valid pointer to a live object. Next, turn related data and functions into a class.

Practice

  1. What does &value produce?
  2. What does *pointer mean in an expression?
  3. Why is nullptr safe to store but unsafe to dereference?
Show reasoned answers
  1. The address of the object named value.
  2. It accesses the object to which the pointer refers.
  3. It represents no object; dereferencing it does not designate a live object and has undefined behavior.

References