A lesson from the Computer Memory module of the bootcamp. About a 12 minute read.
A pointer is an address stored as a number, and a struct is a set of fields placed at fixed offsets from one starting address. Put those together with one hardware rule, that values like to start on round-numbered addresses, and you get the padding bytes that make a struct holding 6 bytes of data take up 12.
A POINTER is a variable whose value is a memory address, 8 bytes on a 64 bit machine. DEREFERENCING it means going to that address and reading what is there, interpreted as whatever type the pointer claims to point to. Pointer arithmetic follows the type: adding 1 to a pointer to int moves it 4 bytes, not 1, which is exactly the base + i x size arithmetic behind array indexing. In C that is not a resemblance, it is the definition: a[i] means *(a + i), so indexing and pointer arithmetic are the same operation written two ways. It is also why a null pointer crashes when you follow it. Address 0 is deliberately left unmapped, so the first touch fails loudly instead of reading something plausible.
The rest of this lesson continues with 6 further sections. See the full curriculum.
Browse all 546 practice puzzles