08Pointers and the heap
The heap
Ask for heap room while the program runs, use it through a pointer, and give it back.
A variable lasts as long as its block or function. Sometimes a program needs room that remains until the programmer decides to let it go, or room whose size is not known before the program runs. That room comes from the heap: ask for a block of it, use it through a pointer, and give it back when done.
Expected output
၄၂
ဗလာ
Asking for heap room and giving it back are statements of their own:
| Zaka | Reads as |
|---|---|
မင်မိုရီ ... ခုစာ နေရာယူ၍ | "taking room worth ... items" |
လိပ်စာကို ပ တွင် သိမ်းထားမည်။ | "and will keep the address in ပ" |
ပ လိပ်စာရှိမင်မိုရီကို | "the block at the address in ပ" |
ပြန်လည်စွန့်လွှတ်မည်။ | "will give back" |
မင်မိုရီ is borrowed from English and means RAM; the native Burmese word မှတ်ဉာဏ်
works the same.
Statements written with …မည် also accept the spoken …မယ်:
Expected output
၇
How much to ask for
Give the size in one of two forms:
| Size | Means |
|---|---|
ကိန်းပြည့် ၁၀ ခုစာ | room for 10 integers |
၈၀ ဘိုက်စာ | room for 80 octets |
Most of the time the first form is the one to use. The second counts octets, the
smallest unit of storage. <kind> ၏ အရွယ်အစား ("the size of the kind") tells how much room,
in octets, one value of a kind takes:
Expected output
၈
၄
၁
၈
So ကိန်းပြည့် ၁၀ ခုစာ and ကိန်းပြည့် ၏ အရွယ်အစား * ၁၀ ဘိုက်စာ ask for the same room.
The count is a လက္ခဏာမဲ့ ကိန်းပြည့် (an unsigned integer). A literal such as ၁၀ fits
on its own. A variable used as a count must have that kind.
When there is no room
If the heap has no room left, the pointer gets ဗလာ instead of an address. A careful
program checks:
Expected output
နေရာ ရပါပြီ
The Zaka VM gives each program 256 MiB of heap; the Explorer gives 16 MiB. zakac run --max-heap <octets> sets another limit.
Giving a block back
Each block from the heap should be given back one time, no more and no less. The pointer that receives the block owns it, and it alone gives the block back. Giving it back:
- sets the owner to
ဗလာ, so using it again finds null instead of a freed block; - does nothing when the owner is
ဗလာto begin with; - is an error the compiler reports while a borrow of the block is still in use; see ownership and borrowing and Pointer mistakes.
The compiler reports a block that a function loses before it returns (ZK0708). For the
blocks that remain when the program ends, run it with zakac run --leaks; it lists each
forgotten block.
Hands on
- Ask for room for three integers, store 1, 2 and 3 in the first one and nowhere else, and print it. (The next page shows how to reach the other two.)
- Ask for the same room with the octet form and
ကိန်းပြည့် ၏ အရွယ်အစား. - Run a program that never gives its block back with
zakac run --leaks.
Next: Pointer arithmetic.