Chapter summary
Zig divides memory into: compile-time memory, the stack, and the heap (dynamic memory). Data known at compile time is embedded directly into the binary.
- If the content isn’t known until runtime, is small, and has a fixed size, it will be stored on the stack.
- If the data is large or variable in size, it will be stored on the heap.
- Accessing the heap is much slower than accessing the stack.
- Pointers let us access and modify data at specific memory addresses. An example of a pointer (specifically a fat pointer) is a slice, which contains both an address and a length.
- Strings in Zig are represented as slices, sentinel-terminated slices, sentinel pointers, or byte arrays.
- To read text from the keyboard, we use stdin, a buffer, and, if needed, a memory allocator to manage dynamic (heap) memory. With defer ...free, the allocated memory is freed when the block ends.
- To compare strings in Zig, we use std.mem.eql instead of ==.