08
Pointers and the heap
Hold the address of a variable, then read and change the variable through it.
Each variable lives somewhere in the computer's RAM, at a numbered place called its address. A pointer is a value that holds an address. A pointer lets a program read or change a variable without using its name: from inside another function, or in RAM that has no name at all.
Expected output
၁၈
၂၀
After the first line, the program never assigns to အသက် with its name. It changes the
variable through the pointer ညွှန်း ("the pointer").
The pointer borrows အသက်: the variable keeps its own storage, and the pointer just
looks at it. A kind that borrows starts with ငှားထားသော ("lent"). The page on ownership
and borrowing tells the rest; until then, each pointer at a variable is
written like this.
| Zaka | Reads as |
|---|---|
ကိန်းပြည့် လိပ်စာ | "integer address": the kind of a pointer to an integer |
ငှားထားသော | "lent": the pointer borrows what it points at |
အသက် ၏ လိပ်စာ | "the address of age" |
ညွှန်း ၏ တန်ဖိုး | "the value at the pointer" |
လိပ်စာ | "address" |
တန်ဖိုး | "value" |
Pointer kinds
A pointer kind is the kind of the value it points to, with လိပ်စာ after it. A pointer
to a pointer adds ၏ လိပ်စာ ("address of") for each further level:
| Kind | Holds the address of |
|---|---|
ငှားထားသော ကိန်းပြည့် လိပ်စာ | an integer, which it borrows |
ငှားထားသော လက္ခဏာမဲ့ ၈ ဘစ် ကိန်းပြည့် လိပ်စာ | an unsigned 8-bit integer, which it borrows |
ငှားထားသော ကိန်းပြည့် လိပ်စာ ၏ လိပ်စာ | a pointer to an integer, which it borrows |
လိပ်စာ | a value of whatever kind (an opaque address) |
Without ငှားထားသော, a pointer kind owns a block of the heap; see The heap.
The older spelling with ၏ at the first level, ကိန်းပြည့် ၏ လိပ်စာ, still works and
means the same as ကိန်းပြည့် လိပ်စာ.
A pointer holds just the addresses of its own kind. The address of a ကိန်းပြည့် does not fit
a pointer to an 8-bit integer:
Expected error
error[ZK0401]: expected &*i8, found &*isize
A bare လိပ်စာ holds the address of a value of whatever kind, unless that value holds an
address itself, but the program cannot read through it until it states what kind of value
is there. The page on pointer arithmetic shows how.
The null address
A pointer that points nowhere holds ဗလာ ("blank"), the null address. A new pointer
variable starts out null. A pointer is not true or false, so compare it with ဗလာ to
test it:
Expected output
ဗလာ
ဘာမှ မညွှန်ပါ
Reading or writing through a null pointer stops the program. See Pointer mistakes.
Short forms
Programmers who know C can write & for "the address of" and * for "the value at":
Expected output
၆
Both spellings mean the same thing, but the word forms and the short forms group in a different order. The page on pointer arithmetic explains the difference.
What has an address
Variables, parameters, ရလဒ် and each value reached through a pointer have addresses.
A number such as ၅ does not, and neither does the result of a sum. A constant has an
address in RAM, but Zaka will not give it out, because writing through it would change
the constant. The same holds for the variable of a counted loop:
Expected error
error[ZK0412]: "ကြီးဆုံး" cannot be changed, so its address cannot be taken
To see these addresses, run a program in the Explorer with debug mode on. Its heap pane shows a block for each global and for each local of each call in progress, and the Block column next to each variable gives the address of its block.
Hands on
- Declare two integers and one pointer. Point at the first, change it through the pointer, then point at the second and change that one.
- Take the address of
၅. Read the message. - Declare a pointer to a pointer,
ငှားထားသော ကိန်းပြည့် လိပ်စာ ၏ လိပ်စာ, that points at aကိန်းပြည့် လိပ်စာvariable, and print၏ တန်ဖိုးthrough it. After the heap, give that variable a block and reach its integer with၏ တန်ဖိုး ၏ တန်ဖိုး.