ECEC 201 · Week 3
Modern Computer: Von Neumann
A pointer is an address.
Week 3 of 10 · Das Frite
ECEC 201 · Week 3 · In class
A pointer is an address.
- An array is a run of addresses, one element after another.
- Adding one to a pointer steps by the size of the element, which is why the step is different for an int, a double, and a char.
ECEC 201 · Week 3 · In class
Reading past the end of an array is the bug this week makes visible.
- The address sanitizer catches the read and names the line.
- Write down the step you expect for each array before the lab; the board either agrees or shows you why not.
ECEC 201 · Week 3 · Lecture
A pointer is an address with a type
&xis the address ofx;*pis the object at the address inp.- The pointer's type says how many bytes live at that address and how to read them.
- An array is its elements one after another, with no gaps between them.
- In most expressions an array name becomes a pointer to its first element:
ameans&a[0].
ECEC 201 · Week 3 · Lecture
p + 1 steps one element
#include <stdio.h>
struct point { short x, y; };
int main(void)
{
struct point pts[3];
struct point *p = pts;
printf("%p\n%p\n", (void *)p, (void *)(p + 1));
printf("elements apart: %td\n", (p + 1) - p);
printf("bytes apart: %td\n", (char *)(p + 1) - (char *)p);
return 0;
}
ECEC 201 · Week 3 · Lecture
a[i] is *(a + i)
| Expression | Means |
|---|---|
a[i] | the element i steps after the first |
&a[i] | a + i, an address |
*p++ | read the element at p, then step p one element |
a + n for a[n] | one past the end: valid to compute, not to read |
ECEC 201 · Week 3 · Lecture
Let the sanitizer find the overrun
- Reading past the end is undefined: it may print a neighbour's bytes and look fine.
gcc -g -fsanitize=addressputs guard bytes around objects and stops at the first bad access.- The report names the access (READ, its size), the line, and the array it ran past.
- Keep
-gon so the report has file and line numbers.
ECEC 201 · Week 3 · Lab
Lab
- Print the address of each element of an int array and of a double array, and the difference between neighbours.
- Walk each array with a pointer, then read one element past the end with `-fsanitize=address` on. Second pass: repeat with a char array and compare its step.
- Assemble the lab hand-in: The program, the address table for the three arrays, and the sanitizer report for the out-of-bounds read.
ECEC 201 · Week 3 · Lab
Hand in
The program, the address table for the three arrays, and the sanitizer report for the out-of-bounds read.
ECEC 201 · Week 3 · Homework
Homework
- For each of the three arrays, write the step you expect between neighbouring addresses and why, before the lab.
ECEC 201 · Week 3 · Homework
Turn in
The three steps and the reason for each.
ECEC 201 · Week 3 · Read
Read
On the lab site: the modules do not grade pointer arithmetic. This week does.
ECEC 201 · Week 3 · Next
Week 4
A running program is a process, and a process has a map of its memory: text for the code, data and bss for globals, the heap for malloc, and the stack for locals.