.\" Made by website/plain/build.mjs from website/words/packed.md; change that file. .TH SG-PACKED 7 2026-11-28 "SG" "SG reference" .SH NAME packed \- a struct with no padding between its fields and an alignment of 1, for a layout a file or a device gives .SH SYNOPSIS .TP \fBpacked struct Header { ... }\fR a struct whose fields lie one after another, with no gap .TP \fBMemory.sizeOf
(), Memory.alignOf
()\fR the sum of the sizes of its fields, and 1 .TP \fBheader.size += 1\fR a field read, written and changed as in any struct .TP \fBvar size = header.size\fR a field copied out, where a borrow of it would be wanted .TP \fBref header.flags\fR a field of one byte, lent as usual .TP \fBHeader.fromBytes(bytes)\fR a header of numbers read out of exactly as many bytes as it takes .SH DESCRIPTION A struct of plain data is laid out as C lays it out: each field at a multiple of its alignment, with empty bytes before it where needed, and the size a multiple of the largest alignment. A \fBpacked struct\fR has no such gaps. Its fields lie one after another, so its size is the sum of their sizes and its alignment is 1: a \fBuint\fR may lie at any byte. It is for a layout that a file, a network packet or a device gives, byte for byte; in C it is a struct between \fB#pragma pack(push, 1)\fR and \fB#pragma pack(pop)\fR\&. .PP It holds plain data only: numbers, \fBbool\fR, \fBchar\fR, raw pointers, \fBCFunc\fR, and arrays, structs and unions of them. A struct inside it keeps its own layout, gaps and all; a packed struct inside another struct needs no gap before it. It has no bit fields, holds no type written with \fBalign(N)\fR and no atomic value, and is not \fBalign(N)\fR itself. .PP Its fields are read, written and changed as in any struct, with no \fBunsafe\fR\&. A field whose type has an alignment above 1 is not lent, since it may lie where that type cannot be reached through a pointer: not with \fBread\fR or \fBref\fR, not by calling a method of it, not by taking a range of an array in it, and not by handing it to C. The hint copies it out; a change is written back whole. A field of one byte, or of a type whose alignment is 1, is lent as usual, and so is the packed struct itself: its own methods take \fBread this\fR and \fBref this\fR\&. .PP \fBHeader.fromBytes(bytes)\fR reads a packed struct out of exactly as many bytes as it takes, as they lie in memory, with no \fBunsafe\fR, when every pattern of bits is a value of each field: numbers, and arrays, structs and unions of them. In raw memory a packed struct may lie at any address, where a struct with a field of a larger alignment stops the program in the interpreter and in a debug build. .SH EXAMPLES .SS "No gaps" .RS 4 .nf struct Loose { byte kind uint length ushort port } packed struct Tight { byte kind uint length ushort port } void main() { print($"{Memory.sizeOf()} {Memory.alignOf()}") print($"{Memory.sizeOf()} {Memory.alignOf()}") var t = Tight { kind = 1, length = 4000000000, port = 8080 } t.length += 1 print($"{t.kind} {t.length} {t.port}") } .fi .RE .PP .RS 4 .nf 12 4 7 1 1 4000000001 8080 .fi .RE .PP \fBLoose\fR keeps \fBlength\fR at a multiple of four, so three bytes stay empty after \fBkind\fR, and two more at the end make the size a multiple of four. \fBTight\fR is the seven bytes of its fields. \fB+=\fR works on its fields as on any other. .SS "A file header" .RS 4 .nf packed struct BmpHeader : Copyable, Formattable, Equatable { ushort kind uint size ushort reserved1 ushort reserved2 uint offset } void main() { var file = List { 0x42, 0x4d, 0x36, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00 } var header = BmpHeader.fromBytes(read file) print(header) print(header.kind == 0x4d42) print($"{header.size - header.offset} bytes of pixels") var copy = header copy.size += 4 print(copy == header) } .fi .RE .PP .RS 4 .nf BmpHeader { kind = 19778, size = 786486, reserved1 = 0, reserved2 = 0, offset = 54 } true 786432 bytes of pixels false .fi .RE .PP The first fourteen bytes of a BMP file: its \fBuint\fR fields lie at bytes 2 and 10, where only a packed struct puts them. \fBfromBytes\fR takes the bytes as they lie in memory; the numbers of a BMP file are little-endian, as the memory of x86-64 and 64-bit Arm is. A packed struct can be \fBFormattable\fR, \fBEquatable\fR and the rest, as any struct can. .SS "Copied out, not lent" .RS 4 .nf packed struct Frame { byte flags uint length byte[4] mark void grow(ref this, uint more) { this.length += more } } void show(read uint n) { print(n) } void raise(ref byte b) { b |= 0x80 } void main() { var frame = Frame { flags = 1, length = 100, mark = byte[4] { 83, 71, 33, 0 } } frame.grow(20) var length = frame.length show(read length) raise(ref frame.flags) print(frame.flags) print(string.fromUtf8(frame.mark[0..3]) ?? "not UTF-8") } .fi .RE .PP .RS 4 .nf 120 129 SG! .fi .RE .PP \fBshow\fR wants a borrow of a \fBuint\fR, and \fBframe.length\fR lies at byte 1, so it gets a copy. \fBflags\fR and \fBmark\fR are bytes, which lie anywhere, so they are lent as they are; the method \fBgrow\fR is lent the whole frame. .SS "Structs inside, and around" .RS 4 .nf struct Plain : Copyable { byte kind ulong stamp } packed struct Record : Copyable { byte tag Plain inside ushort[3] counts } struct Holder { byte flag Record record } void main() { print($"{Memory.sizeOf()} {Memory.sizeOf()} {Memory.sizeOf()}") var r = Record { tag = 7, inside = Plain { kind = 1, stamp = 1700000000 }, counts = ushort[3] { 1, 2, 3 } } r.inside.stamp += 60 r.counts[2] = 65535 var h = Holder { flag = 1, record = r } print(h.record.inside.stamp) print(h.record.counts) } .fi .RE .PP .RS 4 .nf 16 23 24 1700000060 [1, 2, 65535] .fi .RE .PP \fBPlain\fR keeps its seven empty bytes inside \fBRecord\fR, which adds no gap of its own: 1, 16 and 6 make 23. In \fBHolder\fR the record needs no gap before it either, since its alignment is 1. .SS "At any address in raw memory" .RS 4 .nf packed struct Entry : Copyable, Formattable { byte kind uint id ushort size } void main() { var count = 3 var bytes = 1 + count * Memory.sizeOf() var block = Memory.allocate(bytes) var entries = unsafe (raw Entry)(block + 1) unsafe { for i in 0..count { entries[i] = Entry { kind = (byte)i, id = 70000 + (uint)i, size = 512 } } entries[2].size *= 2 } var all = unsafe Memory.view(entries, count) for entry in all { print(entry) } print(bytes) unsafe Memory.free(block) } .fi .RE .PP .RS 4 .nf Entry { kind = 0, id = 70000, size = 512 } Entry { kind = 1, id = 70001, size = 512 } Entry { kind = 2, id = 70002, size = 1024 } 22 .fi .RE .PP The entries start one byte into the block and lie seven bytes apart, as a format on the disk might put them. An alignment of 1 lets them lie there, and lets the view lend them. .SH "WHAT STOPS THE PROGRAM" .SS "A struct that is not packed, where it may not lie" .RS 4 .nf struct Plain { byte kind uint size } void main() { var block = Memory.allocate(16) unsafe { var items = (raw Plain)(block + 1) items[0].size = 3 Memory.free(block) } } .fi .RE .PP .RS 4 .nf panic: raw memory is reached 1 byte(s) past a multiple of 4, the alignment of `Plain`: `Memory.readUnaligned` and `Memory.writeUnaligned` reach it there --> example.sg:10:9 10 | items[0].size = 3 | ^^^^^^^^ .fi .RE .PP \fBPlain\fR has the alignment of its \fBuint\fR, 4, and its first item here lies one byte past a multiple of it. A debug build stops there too; a release build tests nothing. Declared \fBpacked\fR, the same program runs. .SH "WHAT THE COMPILER REFUSES" .SS "A field lent" .RS 4 .nf packed struct Header { byte kind uint size } void show(read uint size) { print(size) } void main() { var header = Header { kind = 1, size = 2 } show(read header.size) } .fi .RE .PP .RS 4 .nf error: a field of a `packed` struct is not lent: `header.size` may lie where a `uint` cannot be reached --> example.sg:12:15 12 | show(read header.size) | ^^^^^^^^^^^ hint: copy it out: `var part = header.size` .fi .RE .SS "A method called on a field" .RS 4 .nf struct Pair { int low int high int sum(read this) { return this.low + this.high } } packed struct Header { byte kind Pair pair } void main() { var header = Header { kind = 1, pair = Pair { low = 1, high = 2 } } print(header.pair.sum()) } .fi .RE .PP .RS 4 .nf error: a field of a `packed` struct is not lent: `header.pair` may lie where a `Pair` cannot be reached a method is lent the value it is called on --> example.sg:17:11 17 | print(header.pair.sum()) | ^^^^^^^^^^^ hint: copy it out: `var part = header.pair`, and write it back whole .fi .RE .SS "A range of an array in it" .RS 4 .nf packed struct Samples { byte tag uint[4] values } uint total(read uint[] xs) { uint sum = 0 for x in xs { sum += x } return sum } void main() { var s = Samples { tag = 1, values = uint[4] { 1, 2, 3, 4 } } print(total(s.values[0..2])) } .fi .RE .PP .RS 4 .nf error: a field of a `packed` struct is not lent: `s.values` may lie where a `uint[4]` cannot be reached --> example.sg:16:17 16 | print(total(s.values[0..2])) | ^^^^^^^^ hint: copy it out: `var part = s.values`, and write it back whole .fi .RE .PP The range would be a slice whose items lie where a \fBuint\fR cannot be reached. .SS "Bit fields" .RS 4 .nf packed struct Header { byte kind : 4 uint size } void main() { } .fi .RE .PP .RS 4 .nf error: a `packed` struct has no bit fields: the compilers of C lay the two out in different ways on Windows bit fields are packed by units of their type, elsewhere bit by bit --> example.sg:2:15 2 | byte kind : 4 | ^^^ .fi .RE .PP A struct of bit fields is laid out as C lays it out on the machine; a packed struct has the same layout on every one. .SS "packed and align together" .RS 4 .nf packed align(8) struct Header { byte kind } void main() { } .fi .RE .PP .RS 4 .nf error: a type is `packed` or `align(N)`, not both `packed` takes the padding out and makes the alignment 1; `align` makes it larger --> example.sg:1:1 1 | packed align(8) struct Header { | ^^^^^^^^^^^^^^^ .fi .RE .SS "A field that owns memory" .RS 4 .nf packed struct Record { byte kind string name } void main() { } .fi .RE .PP .RS 4 .nf error: a `packed` struct holds plain data, and `string` is not plain data is numbers, `bool`, `char`, raw pointers, `CFunc`, and arrays, structs and unions of them: what C lays out --> example.sg:3:5 3 | string name | ^^^^^^ .fi .RE .SH "SEE ALSO" .BR sg-struct (7), .BR sg-union (7), .BR sg-align (7), .BR sg-raw (7), .BR sg-unsafe (7) .PP https://sg\-lang.org/reference/packed.html