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
&xbecomeslea(load effective address): the variable's address is computed and its value is not read.swap(&x, &y)passes the function two addresses inrcxandrdx, not 3 and 8.*ais two steps.mov rax, QWORD PTR 16[rbp]takes the pointer itself (an 8-byte address), andmov eax, DWORD PTR [rax]theintit points to. Square brackets[...]in assembly are the*.- A pointer is a
QWORD(8 bytes) and anintaDWORD(4): the same numberssizeofshowed in step 1.
What to notice (-O0)
*a = *b;is four instructions: takeb, read*b, takea, write to*a.p++insumbecomesadd QWORD PTR ..., 4: a pointer tointmoves not by 1 but by 4 bytes, one element.assert(a != NULL ...)is a comparison with 0:NULLis 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].