.\" Made by website/plain/build.mjs from website/words/raw.md; change that file. .TH SG-RAW 7 2026-11-28 "SG" "SG reference" .SH NAME raw \- an address of C, which owns nothing, may be null, and is not checked by the compiler .SH SYNOPSIS .TP \fBraw T\fR the address of a T, C's T*: it owns nothing and may be null .TP \fBraw void\fR the address of memory of no known type, C's void* .TP \fBraw raw byte\fR the address of an address, C's char .TP \fBMemory.allocate(n)\fR room for n items, all zero, as a raw T .TP \fBunsafe p[i], unsafe (p + n)\fR an item, and the address n items further .TP \fBunsafe (raw U)p\fR the same address, read as another type .TP \fB(nuint)p, unsafe (raw T)n\fR the address as a number, and a number as an address .TP \fBref raw T\fR in a function of C, a variable C writes an address into: C's T .TP \fBunsafe Memory.view(p, n)\fR the n items at p lent as a read T[], with the checks of a slice .TP \fBunsafe Memory.viewRef(p, n)\fR the same as a ref T[], to change them .SH DESCRIPTION A \fBraw T\fR is an address, as C has it: compiled, it is a \fBT*\fR of C and costs what C costs. It owns nothing. Nothing frees what it points at when it ends, nothing stops two copies of it, and nothing stops a use after the memory is gone. Memory from \fBMemory.allocate\fR is given back with \fBMemory.free\fR; memory C gave is given back the way C says, and \fBMemory.free\fR also takes what C's \fBmalloc\fR gave. .PP It differs from the other ways to reach a value. A \fBT*\fR owns its value, is freed where its owner ends, is moved and not copied, and is never \fBnull\fR unless it is a \fBT*?\fR\&. A borrow, \fBread T\fR or \fBref T\fR, is followed by the checker and does not outlive what it borrows. A \fBraw T\fR is copied freely, kept in a field, given back from a function and compared with \fBnull\fR; it is already nullable, so \fBraw T?\fR is not written. .PP A raw pointer points at plain data only: numbers, \fBbool\fR, \fBchar\fR, raw pointers, and arrays and structs of them, laid out as C lays them out. A \fBstring\fR or a \fBList\fR owns memory of its own, so it never lies in raw memory. .PP Making room, keeping a pointer, copying it, comparing two (\fBp == null\fR, \fBp < q\fR) and counting the items between them (\fBq \- p\fR) need no \fBunsafe\fR\&. What touches the memory does: an item, \fBp + n\fR, a cast, \fBMemory.free\fR, \fBMemory.view\fR\&. The page of [\fBunsafe\fR](unsafe.md) lists them all. A raw pointer has no fields of its own (\fBp[0].x\fR, not \fBp.x\fR), no \fB+=\fR, and is not printed, since an address differs from one run to the next. .PP \fBraw T\fR is how functions of C are declared: a parameter or a result of an \fBextern\fR function, \fBref raw T\fR where C writes an address back, and \fBraw sqlite3\fR for a type that C keeps to itself, declared as \fBstruct sqlite3\fR with no body in the \fBextern\fR block. .PP The interpreter checks each item against its block, and stops the program outside it, after \fBMemory.free\fR and through \fBnull\fR\&. Compiled code checks none of this. \fBMemory.view\fR and \fBMemory.viewRef\fR are the way back to checked code: they lend the items as a slice, which the usual checks of an index guard in every build. .SH EXAMPLES .SS "Room for numbers" .RS 4 .nf void main() { raw int nothing = null var temperatures = Memory.allocate(3) print(nothing == null) print(temperatures == null) unsafe { temperatures[0] = 18 temperatures[1] = 21 temperatures[2] = temperatures[0] + temperatures[1] } var copy = temperatures print(unsafe copy[2]) unsafe Memory.free(temperatures) } .fi .RE .PP .RS 4 .nf true false 39 .fi .RE .PP \fBcopy\fR is a second address of the same block, not a second block, so it reads what \fBtemperatures\fR wrote. The block is freed once, by hand. .SS "What C gives back" .RS 4 .nf extern { raw byte strchr(read CString text, int c) CLong strtol(read CString text, ref raw byte end, int base) } void main() { var name = CString.from("report.final.gif") ?? panic("a zero byte inside") var dot = unsafe strchr(read name, (int)'.') if dot != null { print(unsafe string.fromCString(dot) ?? "not UTF-8") } var none = unsafe strchr(read name, (int)'x') print(none == null) var line = CString.from("42 apples") ?? panic("a zero byte inside") raw byte rest = null var count = unsafe strtol(read line, ref rest, 10) print(count) print(unsafe string.fromCString(rest) ?? "not UTF-8") } .fi .RE .PP .RS 4 .nf \&.final.gif true 42 apples .fi .RE .PP \fBstrchr\fR gives an address inside the text, or \fBnull\fR when the character is not there. \fBstrtol\fR writes the address where the number ended into \fBrest\fR, which it is lent as \fBref raw byte\fR: C's \fBchar**\fR\&. .SS "Memory of no known type" .RS 4 .nf extern { raw void malloc(nuint size) raw void memset(raw void target, int value, nuint count) } void main() { var size = (nuint)(4 * Memory.sizeOf()) var block = unsafe malloc(size) if block == null { panic("out of memory") } _ = unsafe memset(block, 0xff, size) var words = unsafe (raw uint)block var bytes = unsafe (raw byte)block print(unsafe words[3]) print(unsafe bytes[0]) unsafe Memory.free(words) } .fi .RE .PP .RS 4 .nf 4294967295 255 .fi .RE .PP A \fBraw void\fR has no items; a cast says what lies there. \fBwords\fR and \fBbytes\fR are the same address read as two types, and \fBMemory.free\fR gives back what \fBmalloc\fR gave. .SS "A checked slice over raw memory" .RS 4 .nf void main() { var latencies = Memory.allocate(5) var view = unsafe Memory.viewRef(latencies, 5) view[0] = 120 view[1] = 45 view[2] = 300 view[3] = 45 view[4] = 90 view.sort() print(view) print(view.length()) unsafe Memory.free(latencies) } .fi .RE .PP .RS 4 .nf [45, 45, 90, 120, 300] 5 .fi .RE .PP After the one \fBunsafe\fR that makes it, \fBview\fR is a \fBref int[]\fR like any other: each index is checked against its length, in compiled code too, and the methods of a slice work on it. .SS "A struct that holds one" .RS 4 .nf struct Canvas : Disposable { raw byte cells nint width nint height Canvas create(nint width, nint height) { var cells = Memory.allocate(width * height) var all = unsafe Memory.viewRef(cells, width * height) for i in 0..all.length() { all[i] = (byte)'.' } return Canvas { cells = cells, width = width, height = height } } void plot(ref this, nint x, nint y) { if x < 0 || x >= this.width || y < 0 || y >= this.height { return } unsafe { this.cells[y * this.width + x] = (byte)'#' } } void show(read this) { var all = unsafe Memory.view(this.cells, this.width * this.height) for y in 0..this.height { var row = all[y * this.width..(y + 1) * this.width] print(string.fromUtf8(row) ?? "not UTF-8") } } void dispose(ref this) { unsafe Memory.free(this.cells) } } void main() { var canvas = Canvas(6, 3) for i in 0..6 { canvas.plot(i, i / 2) } canvas.plot(9, 9) canvas.show() } .fi .RE .PP .RS 4 .nf ##.... \&..##.. \&....## .fi .RE .PP The struct owns the block: \fBdispose\fR frees it once, when \fBcanvas\fR ends, and \fBplot\fR checks a point before the one write that is not checked. \fBshow\fR reads through a view, so a row is a plain slice of bytes. \fBmain\fR sees no pointer. .SH "WHAT STOPS THE PROGRAM" .SS "Freed twice" .RS 4 .nf void main() { var scores = Memory.allocate(3) var other = scores unsafe Memory.free(scores) unsafe Memory.free(other) } .fi .RE .PP .RS 4 .nf panic: `Memory.free` is given memory that was given back already --> example.sg:5:5 5 | unsafe Memory.free(other) | ^^^^^^^^^^^^^^^^^^^^^^^^^ .fi .RE .PP Copying a pointer copies the address, and nothing keeps track of which copy frees it. The interpreter stops here; a compiled build frees the block twice, as C would. .SS "An index outside a view" .RS 4 .nf void main() { var readings = Memory.allocate(3) var view = unsafe Memory.viewRef(readings, 3) view[3] = 1 unsafe Memory.free(readings) } .fi .RE .PP .RS 4 .nf panic: index 3 is out of range: the length is 3 --> example.sg:4:5 4 | view[3] = 1 | ^^^^^^^ .fi .RE .PP Through a view the index is checked in every build, compiled ones too. \fBunsafe readings[3]\fR would be checked only by the interpreter. .SH "WHAT THE COMPILER REFUSES" .SS "A pointer to what owns memory" .RS 4 .nf void main() { var names = Memory.allocate(3) unsafe Memory.free(names) } .fi .RE .PP .RS 4 .nf error: a raw pointer points at plain data, and `string` is not plain data is numbers, `bool`, `char`, raw pointers, and arrays and structs of them: what owns nothing and can be copied byte by byte --> example.sg:2:33 2 | var names = Memory.allocate(3) | ^^^^^^ .fi .RE .SS "A ? on a raw pointer" .RS 4 .nf void main() { raw int? missing = null print(missing is null) } .fi .RE .PP .RS 4 .nf error: a raw pointer can be `null` already, so it is not written with `?` --> example.sg:2:5 2 | raw int? missing = null | ^^^^^^^^ hint: write `raw int`, and compare it with `null` using `==` or `!=` .fi .RE .SS "An item of raw memory lent" .RS 4 .nf struct Point { int x int y } void main() { var points = Memory.allocate(2) var first = unsafe read points[0] print(first.x) unsafe Memory.free(points) } .fi .RE .PP .RS 4 .nf error: a place in raw memory is not lent yet: the checker cannot follow how long it lives --> example.sg:8:24 8 | var first = unsafe read points[0] | ^^^^^^^^^^^^^^ hint: copy it out: `var item = unsafe points[0]` .fi .RE .SS "An item of raw void" .RS 4 .nf extern { raw void malloc(nuint size) } void main() { var block = unsafe malloc(16) var first = unsafe block[0] print(first) unsafe Memory.free(block) } .fi .RE .PP .RS 4 .nf error: a `raw void` points at memory of no known type, so it has no items --> example.sg:7:24 7 | var first = unsafe block[0] | ^^^^^^^^ hint: say what lies there with a cast: `unsafe ((raw byte)block)[0]` .fi .RE .SH "SEE ALSO" .BR sg-unsafe (7), .BR sg-extern (7), .BR sg-export (7), .BR sg-pointer (7), .BR sg-read (7), .BR sg-null (7) .PP https://sg\-lang.org/reference/raw.html