References and Memory Addresses Terminology Reference
This reference provides an overview of references, pointers, and memory terminology you'll encounter in this chapter. Use it as a quick lookup guide.
Value Categories
| Term |
Definition |
Example |
| Lvalue |
Expression that has a persistent identity/memory location |
Variable names, array elements |
| Rvalue |
Temporary expression without persistent identity |
Literal values, temporary calculation results |
| Value Category |
Classification of expressions (lvalue or rvalue) |
x is lvalue, 5 is rvalue |
| Addressable |
Expression whose memory address can be taken |
Lvalues are addressable |
References Basics
| Term |
Definition |
Example |
| Lvalue Reference |
Alias to an existing object |
int& ref = x; creates alias to x |
| Reference Variable |
Variable that refers to another object |
ref acts as another name for x |
| Referent |
Object that a reference refers to |
In int& ref = x;, x is the referent |
| Reference Binding |
Associating reference with object |
int& r = x; binds r to x |
Const References
| Term |
Definition |
Example |
| Lvalue Reference to const |
Reference that cannot modify its referent |
const int& ref = x; |
| Read-Only Reference |
Reference that provides read-only access |
Cannot modify through the reference |
| const Binding |
Reference to const can bind to rvalues |
const int& r = 5; is valid |
| Temporary Lifetime Extension |
Temporary object lives as long as const reference |
Rvalue bound to const reference persists |
Parameter Passing with References
| Term |
Definition |
Example |
| Pass by Reference |
Passing reference to function instead of copy |
void func(int& x) |
| Pass by Value |
Copying argument to function parameter |
void func(int x) |
| Pass by const Reference |
Passing read-only reference |
void func(const int& x) |
| Reference Parameter |
Function parameter that is a reference |
Avoids copying large objects |
Pointers Basics
| Term |
Definition |
Example |
| Pointer |
Variable that stores memory address |
int* ptr = &x; |
| Memory Address |
Numeric location in computer memory |
Address where variable is stored |
| Address-of Operator |
Operator & to get memory address |
&x gets address of x |
| Dereference Operator |
Operator * to access value at address |
*ptr accesses value pointed to |
| Indirection |
Accessing object through pointer |
Using *ptr to access value |
Pointer Operations
| Term |
Definition |
Example |
| Null Pointer |
Pointer that points to nothing |
int* ptr = nullptr; |
| nullptr |
Keyword representing null pointer constant |
Replaces old NULL macro |
| Dangling Pointer |
Pointer to memory that is no longer valid |
Pointer to deleted object |
| Valid Pointer |
Pointer to accessible memory or nullptr |
Safe to use or check |
Pointers and const
| Term |
Definition |
Example |
| Pointer to const |
Pointer that cannot modify pointed-to value |
const int* ptr; |
| const Pointer |
Pointer whose address cannot be changed |
int* const ptr = &x; |
| const Pointer to const |
Both pointer and value are const |
const int* const ptr = &x; |
| Low-Level const |
const on pointed-to type |
const int* has low-level const |
| Top-Level const |
const on pointer itself |
int* const has top-level const |
Parameter Passing with Pointers
| Term |
Definition |
Example |
| Pass by Address |
Passing pointer to function |
void func(int* ptr) |
| Optional Parameter |
Pointer that can be nullptr |
Function checks for nullptr |
| Out Parameter |
Pointer used to return value from function |
Function modifies pointed-to value |
| In Parameter |
Pointer to const for input only |
void func(const int* ptr) |
Return by Reference/Address
| Term |
Definition |
Example |
| Return by Reference |
Returning reference from function |
int& getElement(int index) |
| Return by Address |
Returning pointer from function |
int* findValue(int target) |
| Return by Value |
Returning copy of object |
int getValue() |
| Dangling Reference |
Reference to destroyed local variable |
Undefined behavior |
References vs Pointers
| Term |
Definition |
Example |
| Reference Semantics |
References must be initialized, cannot be null |
Always refers to valid object |
| Pointer Semantics |
Pointers can be null, can be reassigned |
More flexible but less safe |
| Rebinding |
Changing what reference/pointer refers to |
Pointers can, references cannot |
| Null State |
State where pointer points to nothing |
References cannot be null |
Type Deduction with Pointers/References
| Term |
Definition |
Example |
| auto with References |
auto drops reference, use auto& |
int& r = x; auto a = r; (a is int) |
| auto with Pointers |
auto preserves pointer |
int* p = &x; auto a = p; (a is int*) |
| auto with const |
Top-level const dropped, low-level preserved |
const int x = 5; auto y = x; (y is int) |
| Reference Deduction |
Determining reference type automatically |
auto& r = x; deduces int& |
Optional Values
| Term |
Definition |
Example |
| std::optional |
Container for optional value |
std::optional<int> maybeValue; |
| has_value() |
Checks if optional contains value |
if (opt.has_value()) |
| value() |
Accesses value (throws if empty) |
int x = opt.value(); |
| value_or() |
Returns value or default if empty |
int x = opt.value_or(0); |
| std::nullopt |
Represents empty optional |
std::optional<int> opt = std::nullopt; |
Memory Concepts
| Term |
Definition |
Example |
| Object Lifetime |
Duration object exists in memory |
From creation to destruction |
| Stack Memory |
Automatic memory for local variables |
Local variables, function parameters |
| Heap Memory |
Dynamic memory allocated with new |
Manually managed memory |
| Memory Leak |
Allocated memory never freed |
Losing pointer to heap memory |