Lecture 1
Common questions
The things people most often get confused about in pointers.
Why does * mean two different things?Declarations
In a declaration, * is part of the type and means "pointer to". In an expression, *p follows the pointer to the object it points at (dereference). & works the same way: in an expression &x takes an address, while in a declaration int& r makes a reference.
int* p = &x; // declaration: p is a pointer to int
*p = 85; // expression: write to the object p points atWhy does int* p, q; only make one pointer?Declarations
The * belongs to each name, not to int. This line declares p as int* and q as a plain int. Declare one variable per line, or write int *p, *q;.
What is the difference between a null, an uninitialised and a dangling pointer?Pointer states
A null pointer deliberately points at nothing, and you can test for it. An uninitialised local pointer holds garbage. A dangling pointer holds the address of an object that no longer exists. Dereferencing any of them is undefined behaviour, and only the null one can be detected with p != nullptr.
Should I use nullptr, NULL or 0?Pointer states
Use nullptr (C++11). NULL and 0 are integers in disguise, so they can pick the wrong overload, e.g. f(int) instead of f(int*). nullptr has its own type and only converts to pointers.
If I copy a pointer, do I get a copy of the object?Aliasing
No. Copying a pointer copies the address, so both pointers now refer to the same object (an alias). Writing through either one changes that single object. Re-aiming one of them later does not move the other.
int* q = p; // q and p point to the same int
p = &b; // only p moves; q still points to the old targetWhy does p + 1 move 4 bytes instead of 1?Pointer arithmetic
Pointer arithmetic counts in elements. p + n moves by n * sizeof(*p) bytes, so for a 4-byte int that is 4 bytes per step. Subtraction works the same way: last - first gives the number of elements, not bytes.
Is it legal to use a + size, one past the end of the array?Pointer arithmetic
Yes, but only to form and compare it, never to dereference it. That is why loops are written for (int* p = a; p != a + size; ++p). Going any further, like a + size + 1, is already invalid.
I set p = nullptr inside a function. Why is the caller's pointer unchanged?Functions
Pointers are passed by value: the function gets its own copy of the address. Writing *p = ... changes the shared target, but p = ... only changes the copy. To re-aim the caller's pointer, take a reference to the pointer: int*& p.
void reset(int*& p) { p = nullptr; } // now affects the callerWhen should a parameter be a pointer and when a reference?Functions
Both let a function modify the caller's object. Use a pointer when "no object" is a valid input, since it can be nullptr, and check for it. Use a reference when an object must always be given.
Why can't I return the address of a local variable?Functions
Locals die when the function returns, so the caller receives a dangling pointer. Return the value instead. Returning a pointer is only safe when the target outlives the function, such as an element of an array the caller passed in.
double* bad() { double s = 26.48; return &s; } // dangles
double good() { return 26.48; } // fineWhen do I use -> and when .?Structs and classes
Use . on an object and -> on a pointer to an object. p->gpa is shorthand for (*p).gpa. Writing *p.gpa is a bug, because . binds tighter than *.
Why can't a Number<double>* point to a Number<int>?Templates
The compiler generates a separate class for each template argument, and the argument is part of the type. Number<int> and Number<double> are as unrelated as int and std::string.
When should I use std::unique_ptr instead of a raw pointer?Ownership
Use unique_ptr when something must own a dynamically allocated object; it deletes it automatically when it goes out of scope. Use raw pointers to observe objects that someone else owns.
auto p = std::make_unique<int>(42); // freed at scope exitNo questions match.
Ready to check yourself? Take the quiz →