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.

ECEC 201 · Week 3 · In class

Reading past the end of an array is the bug this week makes visible.

ECEC 201 · Week 3 · Lecture

A pointer is an address with a type

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)

ExpressionMeans
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

ECEC 201 · Week 3 · Lab

Lab

  1. Print the address of each element of an int array and of a double array, and the difference between neighbours.
  2. 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.
  3. 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

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.

Arrow keys or space to move, F for full screen, N for speaker notes, Home and End for the first and last slide. Print gives one slide per page.