5.2.2C++ Programming

References — lvalue references, difference from pointers

1,816 words8 min readdifficulty · medium

WHAT is an lvalue reference?

WHY "lvalue"? An lvalue is an expression that names a storage location (it can sit on the left of =). An lvalue reference can only bind to such a thing — you can't say int& r = 5; because 5 has no address to alias. (The exception const int& r = 5; is special and shown later.)


HOW it works under the hood

WHAT actually happens in memory: the compiler typically implements a reference like a pointer that is auto-dereferenced — but semantically there is no separate object. You never see the reference's own address; &r gives you &x.

Figure — References — lvalue references, difference from pointers

References vs Pointers — the core comparison

Feature Reference int& Pointer int*
Is a separate object? No (alias) Yes (holds an address)
Must initialize? Yes No
Can be null? No Yes (nullptr)
Can be reseated? No Yes
Syntax to use r (auto-deref) *p (explicit deref)
Address-of gives the original's address the pointer's own address
Pointer arithmetic No Yes

WHY references exist — pass-by-reference

The biggest use: modify a caller's variable without copying.


Common mistakes (Steel-man + fix)


Active recall

Recall Quick self-test (cover the answers)
  • Can a reference be null? → No.
  • Can a reference be reseated to alias another variable? → No.
  • What does &r return for int& r = x;? → &x (same address).
  • Why does const int& r = 5; compile but int& r = 5; not? → const ref binds a temporary; non-const can't.
  • One reason to prefer const std::string& parameter over std::string? → avoids copying.
Recall Feynman: explain to a 12-year-old

Imagine your dog is named "Rex". Your little sister starts calling him "Buddy". Now the dog has two names but he's still one dog — call either name and the same dog comes running. A reference is the second nickname for a variable: two names, one thing. A pointer is different — it's like a sticky note with the dog's house address written on it. You can erase the note and write a different address (point somewhere else), or leave it blank. A nickname can never be moved to a different dog, and it can never be blank.


Connections

What is an lvalue reference?
An alias — another name for an existing object, sharing the same memory.
Must an lvalue reference be initialized at declaration?
Yes; int& r; is illegal.
Can an lvalue reference be reseated to refer to another object later?
No, never; it stays bound to its initial object.
Can a reference be null?
No — there is no null reference.
For int& r = x;, what does &r evaluate to?
&x — the original's address (the reference has no separate address).
Why does int& r = 5; fail but const int& r = 5; succeed?
Non-const lvalue ref can't bind a temporary; const ref can and extends its lifetime.
Difference between a reference and a T* const pointer?
The const pointer is still a separate object holding an address you can inspect/deref; a reference is not a separate object.
Why prefer const std::string& over std::string as a parameter?
Avoids copying the string and forbids modifying it.
What is the danger of returning a reference to a local variable?
The local dies at return, leaving a dangling reference (undefined behavior).
In swap(int& a, int& b), why no * or & needed inside?
a and b are aliases of the caller's variables, used directly.

Concept Map

is an

shares

requires

constraint

constraint

so

contrasts with

stores address, reseatable, nullable

enables

example

lvalue reference T&

Alias - another name

Same memory box

Must initialize at declaration

Cannot be rebound

Cannot be null

&r gives original address

Pointer T*

Pass-by-reference

swap without copy

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, reference ka matlab hai ek variable ka doosra naam — ek alias. Jaise int& r = x; likha, ab r aur x dono ek hi memory box ke labels hain. r me kuch likho to x bhi change ho jaata hai, kyunki box ek hi hai. Isiliye reference ko initialize karna zaroori hai declaration ke time, aur baad me usko kisi aur variable se bind nahi kar sakte — naam badal nahi sakte. Null bhi nahi ho sakta.

Pointer thoda alag cheez hai. Pointer apne aap me ek alag box hai jisme doosre variable ka address stored hota hai. Tum pointer ko nullptr bana sakte ho, dobara kisi aur variable pe point kara sakte ho, arithmetic kar sakte ho. Use karne ke liye *p se dereference karna padta hai. Reference auto-dereference hota hai, seedha r likho.

Asli faayda reference ka hai pass-by-reference me. Jaise swap(int& a, int& b) — yahan a aur b caller ke variables ke alias ban jaate hain, to function ke andar bina * aur & ke direct swap ho jaata hai. Bade objects ke liye const std::string& s use karo — copy nahi banegi (fast) aur modify bhi nahi kar paoge (safe).

Yaad rakhne wali galti: reference ko mat samajhna "const pointer". Semantically reference koi alag object hai hi nahi — &r tumhe x ka hi address dega, aur local variable ka reference return karna mat, warna woh dangle kar jaata hai (UB). Rule simple: A.R.N. — Alias, Ready (init zaroori), Never change.

Go deeper — visual, from zero

Test yourself — C++ Programming

Connections