Functions & Data Lesson 11 of 17
C++ References, Pointers and Smart Pointers
Understand references as aliases, pointers as addresses, the difference between them, dynamic memory with new and delete, and modern smart pointers like unique_ptr.
C++ has two ways to refer to another variable: references and pointers. References are simpler and safer; pointers are more flexible. Modern C++ also has smart pointers that manage memory for you.
References: another name
#include <iostream>
using namespace std;
int main() {
int score = 80;
int &ref = score; // ref is another name for score
ref += 5;
cout << "score = " << score << endl;
cout << "ref = " << ref << endl;
cout << boolalpha << (&ref == &score) << endl; // same address
return 0;
}
Output
score = 85 ref = 85 true
A reference is an alias. It must be initialized when created, and it always refers to the same variable. There's no "null reference".
Pointers: an address
#include <iostream>
using namespace std;
int main() {
int stock = 40;
int *p = &stock; // p holds the address of stock
cout << "Value via pointer: " << *p << endl;
*p = 35; // change stock through p
cout << "stock = " << stock << endl;
int other = 99;
p = &other; // a pointer can point somewhere else
cout << "Now *p = " << *p << endl;
p = nullptr; // points to nothing
if (p == nullptr) cout << "p is empty" << endl;
return 0;
}
Output
Value via pointer: 40 stock = 35 Now *p = 99 p is empty
In C++, use nullptr for "no address" instead of C's NULL.
References vs pointers
Reference int &r | Pointer int *p | |
|---|---|---|
| Must be initialized | Yes | No (but should be) |
| Can change target | No | Yes |
| Can be null | No | Yes (nullptr) |
| Syntax to use | Like a normal variable | *p to read/write |
Prefer references for function parameters. Use pointers when something may be "nothing" or must change what it points to.
Dynamic memory: new and delete
new creates a value on the heap and returns a pointer; delete frees it:
#include <iostream>
using namespace std;
int main() {
int n = 4;
double *prices = new double[n]{25, 8, 9, 15};
double total = 0;
for (int i = 0; i < n; i++) total += prices[i];
cout << "Total: " << total << endl;
delete[] prices; // [] because it was an array
return 0;
}
Output
Total: 57
Smart pointers
A smart pointer owns an object and deletes it automatically when it's no longer needed:
#include <iostream>
#include <memory>
#include <string>
using namespace std;
struct Student {
string name;
Student(string n) : name(n) { cout << name << " created" << endl; }
~Student() { cout << name << " destroyed" << endl; }
};
int main() {
{
unique_ptr<Student> s = make_unique<Student>("Ana");
cout << "Hello, " << s->name << endl;
} // s goes out of scope here: Ana is deleted automatically
cout << "End of main" << endl;
return 0;
}
Output
Ana created Hello, Ana Ana destroyed End of main
unique_ptr: one owner. The object is deleted when the owner goes away.shared_ptr: several owners; deleted when the last one goes away.- Create them with
make_uniqueandmake_shared. ->accesses members through any pointer, smart or raw.
The ~Student() function is a destructor: it runs automatically when an object is destroyed. You'll see it again in the Classes lessons.
Pointers and arrays
#include <iostream>
using namespace std;
int main() {
int nums[] = {10, 20, 30};
int *p = nums; // points to nums[0]
cout << *(p + 1) << " " << p[2] << endl;
return 0;
}
Output
20 30
Pointer arithmetic works the same as in C. It's important for understanding how things work, but vector and range-based loops make it unnecessary in everyday code.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04