We will use the debugger,
to see how pointers work,
and also practise using gdb to see the states.
By the end you should be able to:
See a pointer as an address,
and an array access as arithmetic on that address
Use GDB to look at what a pointer holds and what lives there
Getting Started
Open the “T8: Debugging Pointers” container,
then change directory into tutorial:
cd ~/tutorial
“A pointer is a variable that stores an address”
Pointers are hard for two reasons:
they’re abstract, and they’re everywhere.
Abstract, because it all comes down to a number you never actually see.
Everywhere, because that number gets used for everything,
a different job each time, so no single picture ever fits.
This tutorial grounds it in the one thing every use shares:
it’s an address.
Other than just saying that “it just stores an address”,
we’ll visualise it in a debugger.
Two questions you can ask about any variable
Every variable has an address and a value.
(gdb) p y what value does it hold?(gdb) p &y where is it stored?
That’s true of int y and of int *p alike.
A pointer isn’t a special kind of thing,
it’s a variable whose value happens to be an address.
We will try to demystify the concept of pointers,
by going through three examples.
The refactor
John wrote a program that prints an array, looping inside main.
It works, and prints all five numbers.
#include <stdio.h>int main(void) { int arr[] = {10, 20, 30, 40, 50}; int n = sizeof(arr) / sizeof(arr[0]); for (int i = 0; i < n; i++) { printf("%d\n", arr[i]); }}
sizeof(arr) / sizeof(arr[0]) is the usual way to get an array’s length.
Here it gives 5.
John decides to tidy up, and pulls the printing out into its own function,
moving the loop word for word.
void print_all(int arr[]) { int n = sizeof(arr) / sizeof(arr[0]); for (int i = 0; i < n; i++) { printf("%d\n", arr[i]); }}
The loop is untouched. But now it prints two numbers, not five.
Printing contiguous memory
You can use GDB to print arrays, or more precisely, print contiguous memory.
Use @ to print several values at once:
(gdb) p arr[0]@5$1 = {10, 20, 30, 40, 50}
Note
Start from the first element, not the pointer. p arr@5 won’t error, it just gives you nonsense.
Use next only as needed to stop at the print_all(arr); line in main
The array should be initialized, but the function call should not have run yet
Print the value of arr: p arr[0]@5
Viewing the types of variables
GDB provides ptype to view the type of variables.
Viewing the types of variables
Let us explore the differences between ptype arr, p arr and p &arr
Open print_all in GDB
Start the program with start, then use next only as needed to stop at the print_all(arr); line in main
The array should be initialized, but the function call should not have run yet
View the output of the following commands: ptype arr, p arr and p &arr
Notice the niceties that GDB has once it knows that arr is of type int [5]
Set a breakpoint at the print_all function: b print_all
Continue to the breakpoint: continue
Run ptype arr, p arr and p &arr again, and compare them with what you saw in main
So… what’s the issue?
sizeof will return the size that the variable takes up.
For instance, if variable is of type int [5], sizeof will be sizeof(int) * 5 (if you try it on WebTop, you should get 20).
Exploring sizeof
Open print_all in GDB
Start the program with start, then use next only as needed to stop at the print_all(arr); line in main
The array should be initialized, but the function call should not have run yet
View the output of the following commands: print sizeof(arr)
Run to the print_all function: advance print_all
View the output of the following commands: print sizeof(arr)
Try to explain to John, why the program prints only 2 numbers.
Solution
sizeof(arr) computes to 8 because arr is a pointer (due to array decay),
and sizeof(arr[0]) is 4 because it is an int. 8 / 4 = 2 by basic algebra.
The swap
John writes a function to swap two numbers.
#include <stdio.h>void swap(int a, int b) { int t = a; a = b; b = t;}int main(void) { int x = 1, y = 2; swap(x, y); printf("%d %d\n", x, y);}
It prints 1 2. Nothing swapped.
John remembers that a function only gets copies of its arguments,
so swap was shuffling copies that get thrown away.
The fix is pointers: pass the addresses, so the function can reach the real x and y.
void swap(int *a, int *b) { int *t = a; a = b; b = t;}
Now it takes pointers, the addresses are correct… and it still prints 1 2.
Open swap in GDB
Step through the program,
and look at how the variables (a, b, x, y, t) change
Draw out a diagram to aid John fix his code.
The list that stays empty
John writes push to add a node to the front of a list.
Everything reads right: allocate a node, link it in, set head to it.
But after two pushes, head back in main is still NULL.
Open push in GDB
Step through the program, and look at how the variables change
Draw out a diagram to aid John fix his code.
Graded Task(s)
In a previous tutorial,
Bob learns that you can easily see the values of variables during runtime.
This time,
he chopped it into pieces,
spread them across a chain of nodes,
and linked them.
If you try to print the variable,
it only shows an address.
He thinks his secret is safe.
Enter the Graded Task Folder
Go to ~/tutorial-graded before starting
Find out the password and pass it into the verify program
Important
Please run the program outside of gdb to complete the task.
Before you leave: verify your progress
Run this command inside your Tutorial environment:
progress
It shows whether you have successfully completed the tutorial and which
individual tasks have been recorded as complete.
If recording fails, run ./verify again outside GDB,
enter the recovered password,
wait for ‘Your completion has been recorded’,
then run progress
The progress command is available only inside a Tutorial environment, not in
the general CS1010 WebTop environment.