BETA Try the new lesson view
Theory

Extra: how does the CPU see it?

Extra. Not graded. What do & and * look like when the processor runs them?

Open the tool window's Assembly tab. On the left is your practice/08/pointers.c.

An address and what is at it

  • &x becomes lea (load effective address): the variable's address is computed and its value is not read. swap(&x, &y) passes the function two addresses in rcx and rdx, not 3 and 8.
  • *a is two steps. mov rax, QWORD PTR 16[rbp] takes the pointer itself (an 8-byte address), and mov eax, DWORD PTR [rax] the int it points to. Square brackets [...] in assembly are the *.
  • A pointer is a QWORD (8 bytes) and an int a DWORD (4): the same numbers sizeof showed in step 1.

What to notice (-O0)

  • *a = *b; is four instructions: take b, read *b, take a, write to *a.
  • p++ in sum becomes add QWORD PTR ..., 4: a pointer to int moves not by 1 but by 4 bytes, one element.
  • assert(a != NULL ...) is a comparison with 0: NULL is just the address 0.

Switch to -O1: swap shrinks to a few instructions, and the pointers are kept in registers (rbx, rsi) without going through memory: *a is one instruction, mov eax, DWORD PTR [rbx].