Lecture 1
Pointers quiz
Answer each question to see whether you got it right and why. Your answers are saved in this browser.
What does this print?
int x = 10; int* p = &x; *p += 5; x *= 2; std::cout << *p;*p += 5makesx15, thenx *= 2makes it 30.pstill points tox, so*psees the new value.After
int* p, q;, what is the type ofq?The
*binds to each declarator, not to the type. Onlypis a pointer;qis a plainint. Declare one variable per line.In
int* p = &x;the*dereferencesp.In a declaration,
*means "pointer to". It only means dereference inside an expression, like*p = 85;.What are
aandbat the end?int a = 10, b = 20; int* p = &a; int* q = p; p = &b; *p = 99;q = pcopies the address, soqpoints toa. Thenpalone is re-aimed atb, and*p = 99writes tob.ais untouched.Given
int a = 5, b = 5; int* p = &a; int* q = &b;, what dop == qand*p == *qevaluate to?p == qcompares addresses, andaandbare different objects.*p == *qcompares the values, which are both 5.What are
aandbafter line 5? (Practice sheet Q1)int a = 4, b = 9; int* p = &a; int* q = p; *q += 3; // 1 q = &b; // 2 *p = *q - 2; // 3 p = q; // 4 *p += a; // 5Line 1:
a = 7. Line 2:qpoints tob. Line 3:a = 9 - 2 = 7. Line 4: both point tob. Line 5:b = 9 + 7 = 16.An uninitialised local pointer like
int* p;is automaticallynullptr.An uninitialised local pointer holds an indeterminate value. Initialise it with
nullptryourself.Checking
if (p != nullptr)before dereferencing protects you from dangling pointers.A dangling pointer still holds a non-null address. Its target is just gone. The null check only catches null pointers.
What does this print?
int a[] = {10, 20, 30, 40}; int* p = a; ++p; p += 2; std::cout << *p;pstarts ata[0],++pmoves toa[1], andp += 2moves toa[3], which holds 40.int a[4]starts at address0x1000andsizeof(int) == 4. What address doesa + 3hold?a + 3moves 3 elements, which is3 * 4 = 12bytes. 12 in hex is0xC, so the address is0x100C.With
int a[] = {10, 20, 30, 40}; int* first = a; int* last = a + 3;, what islast - first?Subtracting pointers counts elements, not bytes. It is 3 even though the addresses differ by 12.
For
int a[4];, which of these is legal?The one-past-the-end pointer
a + 4may be formed and compared but never dereferenced. Anything past it (a + 5) or before the start is already invalid.What is
*p + *qat the end? (Practice sheet Q2)int a[] = {3, 5, 7, 9, 11}; int* p = a + 1; int* q = a + 4; *p = *q - *(p + 1); ++p; --q;a[1] = 11 - 7 = 4, giving{3, 4, 7, 9, 11}. Thenpis at index 2 (7) andqat index 3 (9), so the sum is 16.Calling
void reset(int* p) { p = nullptr; }sets the caller's pointer tonullptr.The pointer is passed by value, so the function changes only its local copy. Use
int*& pto re-aim the caller's pointer.What is the final state? (Practice sheet Q3)
void move(int* p, int* target) { *p += 3; p = target; } int x = 6, y = 10; int* p = &x; move(p, &y); *p += 2;moveadds 3 through the pointer (x = 9), but re-aiming its localpdoes not affect the caller. So*p += 2hitsxagain:x = 11.Which signature lets a function make the caller's pointer point somewhere else?
int*&is a reference to a pointer to int, so assigning topchanges the caller's pointer itself.What is wrong with this function?
double* bad() { double s = 26.48; return &s; }sis a local whose lifetime ends atreturn. The caller gets a dangling pointer. Return the value instead:double good() { return 26.48; }.findMaxreturns a pointer to the first maximum element. The caller runs*findMax(a, 4) = 0;on{4, 9, 2, 9}. What is the array now? (Practice sheet Q4)Using a strict
>comparison keeps the first 9 (index 1). Writing through the returned pointer changes only that element.pis aStudent*. What does*p.gpado?*p.gpaparses as*(p.gpa), and a pointer has no.gpamember. Writep->gpaor(*p).gpa.What is the only difference between
structandclassin C++?Apart from the default access level,
structandclassare the same.Calculator<T>::divide()returnsa / b. What doesCalculator<int>(5, 2).divide()return?Operations follow the rules of
T. Withintthis is integer division, so 5 / 2 is 2. Withdoubleit would be 2.5.Given
Number<int> n(7);, the lineNumber<double>* q = &n;is a compile error.The template argument is part of the type.
Number<int>andNumber<double>are unrelated types.Which statement about
std::unique_ptris true?unique_ptrexpresses exclusive ownership and frees the object when it goes out of scope. It is C++11;make_uniqueis C++14.