You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/langref.html.in
+18-13Lines changed: 18 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -2227,16 +2227,23 @@ or
2227
2227
Unlike normal structs, {#syntax#}packed{#endsyntax#} structs have guaranteed in-memory layout:
2228
2228
</p>
2229
2229
<ul>
2230
-
<li>Fields remain in the order declared, least to most significant.</li>
2230
+
<li>Fields are arranged in the order declared, from the least to the most significant bits of a backing integer.</li>
2231
2231
<li>There is no padding between fields.</li>
2232
-
<li>Zig supports arbitrary width {#link|Integers#} and although normally, integers with fewer
2233
-
than 8 bits will still use 1 byte of memory, in packed structs, they use
2234
-
exactly their bit width.
2232
+
<li>The backing integer has the same bit width as the fields' total bit width.</li>
2233
+
<li>The backing integer is subject to the same rules as any integer, including {#link|alignment|Alignment#},
2234
+
having a maximum bit count of 65535, and the host endianness.
2235
+
On a big endian system, the first declared field will have the highest memory address, and on a little endian system, the lowest.
2235
2236
</li>
2236
-
<li>{#syntax#}bool{#endsyntax#} fields use exactly 1 bit.</li>
2237
+
<li>Due to {#link|alignment|Alignment#}, the backing integer may require more memory that its bit width. {#link|@bitSizeOf|@bitSizeOf#} and {#link|@sizeOf|@sizeOf#} can interrogate the difference.</li>
2238
+
<li>Field access and assignment can be understood as shorthand for bitshifts on the backing integer.</li>
2239
+
<li>An {#link|integer|Integers#} field uses exactly as many bits as its bit width. For example, a {#syntax#}u5{#endsyntax#} will use 5 bits of the backing integer.</li>
2240
+
<li>A {#link|bool|Primitive Types#} field uses exactly 1 bit.</li>
2237
2241
<li>An {#link|enum#} field uses exactly the bit width of its integer tag type.</li>
2238
2242
<li>A {#link|packed union#} field uses exactly the bit width of the union field with
2239
2243
the largest bit width.</li>
2244
+
<li>A {#syntax#}packed struct{#endsyntax#} field, when within a {#syntax#}packed struct{#endsyntax#}, uses exactly the bit width of its backing integer. For example,
2245
+
a {#syntax#}packed struct{#endsyntax#} field having backing integer {#syntax#}u17{#endsyntax#} uses 17 bits of its parent's backing integer.
2246
+
</li>
2240
2247
<li>Packed structs support equality operators.</li>
2241
2248
</ul>
2242
2249
<p>
@@ -2247,8 +2254,7 @@ or
2247
2254
{#code|test_packed_structs.zig#}
2248
2255
2249
2256
<p>
2250
-
The backing integer is inferred from the fields' total bit width.
2251
-
Optionally, it can be explicitly provided and enforced at compile time:
2257
+
The backing integer can be inferred or explicitly provided. When inferred, it will be unsigned. When explicitly provided, its bit width will be enforced at compile time to exactly match the total bit width of the fields:
2252
2258
</p>
2253
2259
{#code|test_missized_packed_struct.zig#}
2254
2260
@@ -2295,12 +2301,11 @@ or
2295
2301
{#code|test_packed_struct_equality.zig#}
2296
2302
2297
2303
<p>
2298
-
Using packed structs with {#link|volatile#} is problematic, and may be a compile error in the future.
0 commit comments