Unit 3: Memory Management, Pointers & ReferencesLesson #10 / 20

Lesson 10: References vs Pointers: Aliasing, Const References & Safety

NSNextSemLast Updated: 5 Aug, 2026

🔗 References vs Pointers: Head-to-Head Comparison

Both pointers and references allow you to manipulate memory across function scopes, but they differ significantly in safety and syntax.


📊 Comparative Breakdown

FeaturePointer (int ptr)Reference (int &ref)
InitializationOptional at declaration (int p = nullptr;)Mandatory at declaration (int &r = x;)
Null AbilityCan be nullptrCannot be null
Re-assignmentCan point to different variables over timePermanent alias (cannot be rebound)
SyntaxRequires to dereferenceUses standard variable syntax directly

💻 Code Example: Const Reference Efficiency

C++ Source Code (main.cpp)
#include <iostream>
#include <string>
// const std::string & avoids copying a 10,000 character string!
void printLogMessage(const std::string &msg) {
std::cout << "[LOG]: " << msg << std::endl;
}
int main() {
std::string systemEvent = "Windows 11 Kernel Process Initialized Successfully";
printLogMessage(systemEvent);
return 0;
}
Windows 11 Pro Terminal Execution OutputWindows 11 Pro Verified
Administrator: Windows 11 Pro Command Prompt (g++ GCC 13.2.0)

Microsoft Windows [Version 10.0.22631.3007] (Windows 11 Pro x86_64)

(c) Microsoft Corporation. All rights reserved.

C:\Users\Student\CPP_Project>#include <iostream>

C:\Users\Student\CPP_Project> g++ -o main.exe main.cpp C:\Users\Student\CPP_Project> main.exe Hello World from C++ on Windows 11 Pro! [Process exited with code 0 in 0.001 seconds]

Interactive Knowledge Check

Test your understanding of Lesson #10 concepts

Which of the following statements about C++ References (int &ref = x) is TRUE?