⚡ Zig Guide LiveUnofficialbut fully verified
✓ Zig 0.17.0-dev.1516+8a4b5424dWhat's newOn an older Zig?

Systems from Scratch

Systems programming taught from zero, with Zig as the vehicle instead of C. Each chapter asks one question, answers it in plain terms, and then hands you a complete program that proves the answer. If you have written C before, the last part of each page says how the same idea is spelled there. If you have not, nothing here needs it. The order is the one a C course takes. Nothing earlier in the guide is a prerequisite, so read it whenever the machine underneath starts mattering, including first.

  1. A type is not a property of the bytes. It is an agreement about how to read them, and the same four bytes can be read two ways without converting anything.
  2. u8 and u32 are not styles of writing a number. They are different amounts of memory, and the difference is a million bytes when you have a million of them.
  3. An address is an ordinary number. @intFromPtr does no work; it shows you the number the pointer was already holding.
  4. A call's locals are handed back when it returns, and the next call gets the same bytes. That reuse is the dangling pointer, and nothing about the pointer changed.
  5. Undefined behaviour is not a crash and not a garbage value. It is a promise you made to the compiler, which is why breaking it can change code somewhere else.

11 chapters.

  • Everything Is BytesA type is not a property of the bytes. It is an agreement about how to read them.
  • Numbers Have a SizeA u8 counts to 255 and stops. What happens next is a decision you make.
  • Memory Is NumberedAn address is an ordinary number, and a pointer is a variable holding one.
  • The StackWhere locals live, and why a pointer to one stops being safe the moment the call returns.
  • Who Owns This MemoryMemory that outlives a call has to be asked for, and somebody has to give it back.
  • Off the EndA length is a value, and the difference between languages is who is made to check it.
  • When the Checks Are OffWhat undefined behaviour actually is, and what each build mode promises.
  • Strings Are BytesThere is no string type. A literal is bytes, with a zero on the end.
  • Structs Are LayoutFields sit next to each other, and the size is not the sum of the parts.
  • Errors Are ValuesThere is no exception mechanism underneath you, so failure has to be part of what a function returns.
  • Talking to the Operating SystemYour program is handed streams it did not open, and owes a status back.