The Runtime Theory
easyApplicationFoundations

Pointers vs References vs Values

The trade-offs between passing by value, by pointer, and by reference — semantics, performance, and null safety.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team1 min read

Pointers vs References vs Values

The Three Ways to Pass Data

cpp
void byValue(int x);       // copies the value
void byPointer(int* p);    // passes the address
void byReference(int& r);  // passes an alias

Comparison Table

DimensionValuePointerReference
Copy costFull copyNoneNone
Null safetyAlways validCan be nullptrNever null
ReassignmentN/ACan point elsewhereBound at initialization
Syntaxf(x)f(&x)f(x)
Accessx*pr

When to Use Each

UseMethod
Small types (int, char, pointers)Value — copy is cheaper than indirection
Large types you don't modifyConst reference (const T&) — avoids copy, guarantees no mutation
Large types you modifyReference (T&) — avoids copy, modifications propagate
Nullable parameterPointer (T*) — pass nullptr when absent
Optional parameterPointer (T*) — default to nullptr

Follow-up Questions

Can a reference be null?

No. A reference must be initialized when declared and cannot be reassigned. However, a reference to a null pointer is undefined behavior:

cpp
int* p = nullptr;
int& r = *p;  // undefined behavior — dereferencing nullptr

When should you avoid passing by reference?

Avoid it for small primitive types (int, char, double) — copying them is cheaper than following a pointer. Also avoid non-const references when the function doesn't need to modify the argument — prefer const T& for read-only large objects.

This answer walks

Practice follow-ups

  1. 01When should you avoid passing by reference?
  2. 02Can a reference be null?

One dispatch a week

The trace behind each question, the tradeoff that explains it, and one technical dispatch per week — no noise.

One technical dispatch per week. No noise.

Not started

Sign in to save your learning progress.

Sign in to save