.\" Made by website/plain/build.mjs from website/words/nuint.md; change that file. .TH SG-NUINT 7 2026-11-28 "SG" "SG reference" .SH NAME nuint \- an integer without a sign, as wide as an address; C's size_t, and an address as a number .SH SYNOPSIS .TP \fBnuint\fR an integer without a sign, as wide as an address: 64 bits on today's computers .TP \fBnuint.MinValue, nuint.MaxValue\fR 0 and the machine's largest address .TP \fBnuint strlen(read CString text)\fR in an extern block, C's size_t .TP \fB(nuint)xs.length(), (nuint)Memory.sizeOf()\fR a length or a size, for C .TP \fB(nint)n\fR a nuint as an index or a length .TP \fB(nuint)p\fR the address a raw pointer holds, as a number .TP \fBunsafe (raw T)n\fR a number as an address .TP \fBn.tryMultiply(size), n.tryToNuint()\fR arithmetic and conversions that never panic .SH DESCRIPTION A \fBnuint\fR is an integer without a sign, as wide as an address on the machine: 64 bits on today's computers, from 0 to 18 446 744 073 709 551 615. It is C's \fBsize_t\fR and \fBuintptr_t\fR: the count and the size that functions of C take and give, and an address read as a number. .PP SG itself counts with [\fBnint\fR](nint.md): a length, an index and \fBMemory.sizeOf()\fR are \fBnint\fRs, so they can go below zero in arithmetic and say so. A \fBnuint\fR is for the edge with C and for addresses. On the way to C a length is written \fB(nuint)xs.length()\fR; on the way back a count is written \fB(nint)n\fR, which also makes it an index. An index never takes a \fBnuint\fR on its own. .PP A \fBbyte\fR, a \fBushort\fR and a \fBuint\fR become a \fBnuint\fR on its own, and a \fBnuint\fR becomes a \fBulong\fR\&. Nothing with a sign does: a \fBnint\fR or an \fBint\fR goes to a \fBnuint\fR with \fB(nuint)\fR, which stops the program on a negative value, and the two do not mix in arithmetic. A \fBnuint\fR has no \fB\-x\fR, since it is never negative. .PP \fB(nuint)p\fR reads the address of a raw pointer as a number, with no \fBunsafe\fR: it reads no memory. The way back, \fBunsafe (raw T)n\fR, makes a pointer of a number, which may be no address of anything. An address is printed only as such a number, and it differs from one run to the next. .PP Overflow follows the rule of every integer: \fB0 \- 1\fR stops the program in the interpreter, in tests and in a debug build, and wraps to \fBnuint.MaxValue\fR in a release build. A size computed for C, \fBcount * size\fR, is computed with \fBtryMultiply\fR where it can be large, so a huge count gives \fBnull\fR and not a small block. Like \fBnint\fR, a \fBnuint\fR has no \fBtoLittleEndian\fR or \fBfromBigEndian\fR\&. .SH EXAMPLES .SS "Sizes, and back to an index" .RS 4 .nf void main() { nuint size = 4096 uint pages = 3 size *= pages print(size) ulong wide = size print(wide + 1) print(nuint.MaxValue) var xs = List { 4, 5, 6 } nuint at = 2 print(xs[(nint)at]) var length = (nuint)xs.length() print(length) } .fi .RE .PP .RS 4 .nf 12288 12289 18446744073709551615 6 3 .fi .RE .PP \fBpages\fR, a \fBuint\fR, becomes a \fBnuint\fR on its own, and \fBsize\fR becomes a \fBulong\fR\&. An index is an \fBnint\fR, so \fBat\fR is written \fB(nint)at\fR; a length goes the other way with \fB(nuint)\fR\&. .SS "C's size_t" .RS 4 .nf extern { nuint strlen(read CString text) int snprintf(raw byte target, nuint size, read CString format, ...) } void main() { var name = CString.from("Pasha") ?? panic("a zero byte inside") nuint length = unsafe strlen(read name) print(length) nuint room = 8 var buffer = Memory.allocate((nint)room) var format = CString.from("%s is %d") ?? panic("a zero byte inside") var wanted = unsafe snprintf(buffer, room, read format, read name, (int)7) print(wanted) print(unsafe string.fromCString(buffer) ?? "not UTF-8") unsafe Memory.free(buffer) } .fi .RE .PP .RS 4 .nf 5 10 Pasha i .fi .RE .PP \fBstrlen\fR gives a \fBsize_t\fR, so it is declared to give a \fBnuint\fR\&. \fBsnprintf\fR writes at most \fBroom\fR bytes, the last of them a zero, and gives the length it wanted, 10. \fBMemory.allocate\fR counts items as an \fBnint\fR, so the room is written \fB(nint)room\fR there. .SS "A count and a size for C" .RS 4 .nf extern { void qsort(raw void base, nuint count, nuint size, CFunc compare) } struct Point : Formattable { int x int y } export int byX(raw void a, raw void b) { var p = unsafe ((raw Point)a)[0] var q = unsafe ((raw Point)b)[0] return p.x - q.x } void main() { var points = List { Point { x = 3, y = 0 }, Point { x = -1, y = 5 }, Point { x = 2, y = 2 } } var count = (nuint)points.length() var size = (nuint)Memory.sizeOf() print($"{count} items of {size} bytes") unsafe qsort((raw void)points.pointer(), count, size, byX) print(points) } .fi .RE .PP .RS 4 .nf 3 items of 8 bytes [Point { x = -1, y = 5 }, Point { x = 2, y = 2 }, Point { x = 3, y = 0 }] .fi .RE .PP \fBqsort\fR takes the count of items and the size of one as \fBsize_t\fR\&. Both are \fBnint\fRs in SG, and \fB(nuint)\fR hands them to C. .SS "A size that does not fit" .RS 4 .nf nuint? bytesFor(nuint count, nuint size) { return count.tryMultiply(size) } void main() { print(bytesFor(1000, 8) ?? 0) print(bytesFor(nuint.MaxValue / 4, 8) ?? 0) nuint big = nuint.MaxValue / 4 print(big.wrappingMultiply(8)) print(big.saturatingMultiply(8)) nint below = -3 print(below.tryToNuint() ?? 0) print(below.wrappingToNuint()) } .fi .RE .PP .RS 4 .nf 8000 0 18446744073709551608 18446744073709551615 0 18446744073709551613 .fi .RE .PP \fBtryMultiply\fR gives \fBnull\fR where \fBcount * size\fR would not fit; \fBwrappingMultiply\fR keeps the low bits, a block far too small. \fBtryToNuint\fR gives \fBnull\fR for a negative number, and \fBwrappingToNuint\fR its bits. .SS "An address as a number" .RS 4 .nf nuint alignUp(nuint at, nuint align) { return (at + align - 1) / align * align } void main() { print(alignUp(13, 8)) print(alignUp(16, 8)) var block = Memory.allocate(64) nuint start = (nuint)block var align = (nuint)Memory.alignOf() print(start % align) nuint next = alignUp(start + 3, align) print(next - start) var slot = unsafe (raw long)next unsafe { slot[0] = 1234567890123 } var same = unsafe (raw long)(block + 8) print(unsafe same[0]) unsafe Memory.free(block) } .fi .RE .PP .RS 4 .nf 16 16 0 8 1234567890123 .fi .RE .PP \fB(nuint)block\fR reads the address with no \fBunsafe\fR\&. The block starts at a multiple of 8, so the next place for a \fBlong\fR after byte 3 is byte 8. \fBunsafe (raw long)next\fR makes that number a pointer again, the same as \fBblock + 8\fR read as a \fBlong\fR\&. .SH "WHAT STOPS THE PROGRAM" .SS "A negative number as a nuint" .RS 4 .nf nuint sizeOf(nint count) { return (nuint)count } void main() { print(sizeOf(-1)) } .fi .RE .PP .RS 4 .nf panic: value -1 does not fit in nuint --> example.sg:2:12 2 | return (nuint)count | ^^^^^^^^^^^^ hint: `tryToNuint()` gives `null` when the value does not fit, and `wrappingToNuint()` cuts it to size .fi .RE .SS "Below zero" .RS 4 .nf nuint left(nuint room, nuint used) { return room - used } void main() { print(left(8, 10)) } .fi .RE .PP .RS 4 .nf panic: overflow: 8 - 10 does not fit in nuint --> example.sg:2:12 2 | return room - used | ^^^^^^^^^^^ hint: `trySubtract` gives `null` instead, and `wrappingSubtract` wraps around .fi .RE .PP In a release build it wraps to 18 446 744 073 709 551 614. .SH "WHAT THE COMPILER REFUSES" .SS "A nuint as an index" .RS 4 .nf void main() { var xs = List { 4, 5, 6 } nuint at = 2 print(xs[at]) } .fi .RE .PP .RS 4 .nf error: expected `nint`, found `nuint` --> example.sg:4:14 4 | print(xs[at]) | ^^ hint: convert with `(nint)at`; a value that does not fit panics, or is cut to size in a release build .fi .RE .SS "A nuint and an int" .RS 4 .nf void main() { nuint size = 16 int extra = 4 print(size + extra) } .fi .RE .PP .RS 4 .nf error: `nuint` and `int` do not mix: neither holds every value of the other --> example.sg:4:11 4 | print(size + extra) | ^^^^^^^^^^^^ hint: convert one side to the type of the other: `(nuint)extra`; a value that does not fit panics, or is cut to size in a release build .fi .RE .SS "A length as a nuint" .RS 4 .nf void main() { var xs = List { 4, 5, 6 } nuint count = xs.length() print(count) } .fi .RE .PP .RS 4 .nf error: expected `nuint`, found `nint` --> example.sg:3:19 3 | nuint count = xs.length() | ^^^^^^^^^^^ hint: convert with `(nuint)xs.length()`; a value that does not fit panics, or is cut to size in a release build .fi .RE .SS "A negative nuint" .RS 4 .nf void main() { nuint size = 16 print(-size) } .fi .RE .PP .RS 4 .nf error: operator `-` is not defined for `nuint`: a `nuint` is never negative --> example.sg:3:11 3 | print(-size) | ^^^^^ hint: convert first: `-(long)size` .fi .RE .SS "An address in a long" .RS 4 .nf void main() { var block = Memory.allocate(4) long at = (long)block print(at > 0) unsafe Memory.free(block) } .fi .RE .PP .RS 4 .nf error: an address is read as a number as wide as an address, and `long` is not one --> example.sg:3:15 3 | long at = (long)block | ^^^^^^^^^^^ hint: `(nuint)block`, or `(nint)block` .fi .RE .SS "A pointer made from a long" .RS 4 .nf void main() { long at = 4096 var p = unsafe (raw int)at print(p == null) } .fi .RE .PP .RS 4 .nf error: a cast to `raw int` reads a raw pointer as another, or an address as a pointer, and this is `long` --> example.sg:3:29 3 | var p = unsafe (raw int)at | ^^ hint: an address is a `nint` or a `nuint`: `(raw int)(nuint)at` .fi .RE .SH "SEE ALSO" .BR sg-nint (7), .BR sg-uint (7), .BR sg-ulong (7), .BR sg-raw (7), .BR sg-extern (7), .BR sg-unsafe (7) .PP https://sg\-lang.org/reference/nuint.html