.\" Made by website/plain/build.mjs from website/words/pointer.md; change that file. .TH SG-POINTER 7 2026-11-28 "SG" "SG reference" .SH NAME T* \- a pointer that owns a value on the heap .SH SYNOPSIS .TP \fBT*\fR a pointer that owns a T on the heap .TP \fBnew x\fR puts x on the heap and gives its T* .TP \fBT*?\fR the same, or null: a link that may be missing .TP \fBp.field, p.method()\fR a field or a method of the value it points to .TP \fBread p, ref p\fR a borrow of the value, where a read T or a ref T goes .TP \fBvar q = p\fR moves the pointer; p is not used after .TP \fBnode.next.take()\fR the pointer in a T*? field, with null left in its place .TP \fBshared T, weak T\fR several owners, and a look that owns nothing: see shared .SH DESCRIPTION A \fBT*\fR owns one value on the heap. \fBnew\fR makes it, and each \fBnew\fR in the code is one allocation: a value is never put on the heap by itself. Compiled, a \fBT*\fR is one pointer, and a \fBT*?\fR is the same pointer with \fBnull\fR as the address zero, so a link that may be missing costs nothing more. .PP Through a \fBT*\fR, fields and methods are reached with \fB\&.\fR, as on the value itself, and \fBprint\fR writes the value, not an address. \fB==\fR and the order compare the values two pointers own, never their addresses. A borrow of a \fBT*\fR goes where a borrow of a \fBT\fR is wanted, so a function that takes \fBread Point\fR takes the point behind a \fBPoint*\fR too. .PP A \fBT*\fR has one owner, as every value does. Giving it to a variable or a function moves the pointer, not the value: a large value changes hands for the price of one pointer and is never copied. The value is freed, with everything it owns, where its last owner ends: at the end of a block, when the pointer is written over, or in the function it was given to. There is no \fBdelete\fR and no garbage collector. A chain of values linked by one field is freed in a loop, so a long one does not run out of stack. .PP Most values need no pointer: a struct lives where its variable is, in a list or inside another struct, with no allocation. A pointer is needed where a type holds itself (the node of a list, a tree, an expression), since such a type would have no size; it also keeps a large value from being copied byte by byte each time it moves. .PP Since each value has one owner, owning pointers make chains and trees, never circles. A value reached from several places, or one that looks back at what holds it, has several owners: \fBshared T\fR, with a \fBweak T\fR for the link back. .SH EXAMPLES .SS "A value, and a value behind a pointer" .RS 4 .nf struct Point : Formattable { int x int y } void main() { var a = Point { x = 1, y = 2 } var b = new Point { x = 3, y = 4 } b.x += a.x print(a) print(b) print(b.x * b.y) } .fi .RE .PP .RS 4 .nf Point { x = 1, y = 2 } Point { x = 4, y = 4 } 16 .fi .RE .PP \fBa\fR is a \fBPoint\fR and lives in \fBmain\fR; \fBb\fR is a \fBPoint*\fR and points to a point on the heap. Both are read and written the same way. .SS "The pointer moves, the value stays" .RS 4 .nf struct Image : Disposable { string name byte[65536] pixels void dispose(ref this) { print($"free {this.name}") } } Image* load(string name) { var image = new Image { name = name, pixels = byte[65536] } image.pixels[0] = 200 return image } Image* brighten(Image* image) { image.pixels[0] += 50 return image } void main() { var photo = load("cat.gif".clone()) var done = brighten(photo) print(done.pixels[0]) print("end of main") } .fi .RE .PP .RS 4 .nf 250 end of main free cat.gif .fi .RE .PP The image is made once. \fBload\fR gives back its pointer, \fBbrighten\fR takes it and gives it back, and \fBdone\fR takes it from there: 64 KB change hands as one pointer each time. It is freed once, where its last owner, \fBdone\fR, ends. .SS "A link that may be missing" .RS 4 .nf struct Frame { string name Frame*? below } struct Stack { Frame*? top int depth void push(ref this, string name) { this.top = new Frame { name = name, below = this.top.take() } this.depth += 1 } string? pop(ref this) { var frame = this.top.take() ?? return null this.top = frame.below.take() this.depth -= 1 return frame.name } } void main() { var calls = Stack { top = null, depth = 0 } calls.push("main".clone()) calls.push("parse".clone()) calls.push("readLine".clone()) print(calls.depth) print(calls.pop() ?? "empty") print(calls.pop() ?? "empty") print(calls.depth) } .fi .RE .PP .RS 4 .nf 3 readLine parse 1 .fi .RE .PP \fBFrame*? below\fR is the frame under this one, or \fBnull\fR at the bottom. \fBtake()\fR moves a pointer out of a field and leaves \fBnull\fR there, which is how a link is moved through a \fBref this\fR\&. \fBpop\fR then owns the frame, gives its name back, and the frame is freed when \fBpop\fR ends. .SS "A type that holds itself" .RS 4 .nf enum Expr { Number(double value) Add(Expr* left, Expr* right) Times(Expr* left, Expr* right) } double eval(read Expr e) { return match e { Number(v) => v Add(l, r) => eval(read l) + eval(read r) Times(l, r) => eval(read l) * eval(read r) } } void main() { var sum = new Expr.Add(new Expr.Number(2), new Expr.Number(3)) var e = Expr.Times(sum, new Expr.Number(4)) print(eval(read e)) } .fi .RE .PP .RS 4 .nf 20 .fi .RE .PP An \fBExpr\fR holds other \fBExpr\fRs, so it holds them behind pointers. \fBe\fR itself is a value in \fBmain\fR; its parts are on the heap, and they are freed with it. .SS "A long chain" .RS 4 .nf struct Node { int value Node*? next } void main() { Node*? list = null for i in 0..1000000 { list = new Node { value = i, next = list } } var at = read list long sum = 0 while at is not null { sum += at.value at = read at.next } print(sum) } .fi .RE .PP .RS 4 .nf 499999500000 .fi .RE .PP Each new node takes the list so far as its \fBnext\fR\&. \fBat\fR only borrows, so the walk moves nothing. A million nodes are freed when \fBlist\fR ends, one after another in a loop, with no recursion that could run out of stack. .SH "WHAT THE COMPILER REFUSES" .SS "A type that holds itself, not behind a pointer" .RS 4 .nf enum Expr { Number(double value) Add(Expr left, Expr right) } void main() { var e = Expr.Number(1) print(e is Number) } .fi .RE .PP .RS 4 .nf error: enum `Expr` contains itself, so it has no finite size --> example.sg:3:9 3 | Add(Expr left, Expr right) | ^^^^ hint: put it behind a pointer: `Expr* left` .fi .RE .SS "Two owners of one value" .RS 4 .nf struct Folder { string name Folder*? parent } void main() { var home = new Folder { name = "home", parent = null } var docs = new Folder { name = "docs", parent = home } var notes = new Folder { name = "notes", parent = home } print(docs.name) print(notes.name) } .fi .RE .PP .RS 4 .nf error: `home` was moved and can no longer be used --> example.sg:9:55 9 | var notes = new Folder { name = "notes", parent = home } | ^^^^ it was moved here: --> example.sg:8:53 8 | var docs = new Folder { name = "docs", parent = home } | ^^^^ .fi .RE .PP \fBdocs\fR owns \fBhome\fR now, and a value has one owner. Two folders that point to one parent need \fBshared Folder\fR for the parent, and a link to the parent that does not own it is a \fBweak Folder?\fR\&. .SS "A value put inside itself" .RS 4 .nf struct Node { int value Node*? next } void main() { var last = new Node { value = 1, next = null } last.next = last print(last.value) } .fi .RE .PP .RS 4 .nf error: `last.next` is reached through `last`, which the right side moves the right side is computed first, so nothing is left to write into; a value cannot be put inside itself --> example.sg:8:5 8 | last.next = last | ^^^^^^^^^ it is moved here: --> example.sg:8:17 8 | last.next = last | ^^^^ .fi .RE .PP The right side moves \fBlast\fR into the field, so there is no \fBlast\fR left to hold the field. A list that goes round has no last owner to free it; it is made of \fBshared\fR values with a \fBweak\fR link back. .SS "A weak look at a T*" .RS 4 .nf struct Folder { string name weak Folder? parent } void main() { var home = new Folder { name = "home", parent = null } var docs = new Folder { name = "docs", parent = weak home } print(docs.name) } .fi .RE .PP .RS 4 .nf error: `weak` needs a `shared` value, and this is `Folder*` a `Folder*` has one owner; a value with several is made with `new shared`: `shared Folder` --> example.sg:8:58 .fi .RE .PP \fBnew shared Folder { ... }\fR for both folders makes the program right. .SS "A link used without a test" .RS 4 .nf struct Node { int value Node*? next } int second(read Node*? list) { return list.next.value } void main() { var a = new Node { value = 1, next = null } print(second(read a)) } .fi .RE .PP .RS 4 .nf error: this is `Node*?` and may be `null`; `.next` needs a value --> example.sg:7:12 7 | return list.next.value | ^^^^ hint: test it first: `if list is not null { ... }` .fi .RE .SS "A method that takes the value, called through the pointer" .RS 4 .nf struct Point { int x int y Point moved(this, int dx) { return Point { x = this.x + dx, y = this.y } } } void main() { var p = new Point { x = 1, y = 2 } var q = p.moved(3) print(q.x) print(p.x) } .fi .RE .PP .RS 4 .nf error: `p` was moved and can no longer be used --> example.sg:14:11 14 | print(p.x) | ^ it was moved here: --> example.sg:12:13 12 | var q = p.moved(3) | ^ .fi .RE .PP \fBmoved\fR takes \fBthis\fR by value, so the call moves the point out of the pointer and uses it up. A method that only looks takes \fBread this\fR, and then \fBp\fR stays. .SH "SEE ALSO" .BR sg-new (7), .BR sg-nullable (7), .BR sg-shared (7), .BR sg-weak (7), .BR sg-struct (7), .BR sg-enum (7), .BR sg-raw (7) .PP https://sg\-lang.org/reference/pointer.html