authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-30 16:27:12-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-30 16:27:12-07:00
logffaeb4533395fafcf020d0aca2f2efd58e48a384
tree6a6480fd4ec2c8ef03f2a8b790bf425250a3c2cd
parent1880c799aebed7efe524c040998b27a2b361ff43

langref: clarify struct memory layout


1 files changed, 6 insertions(+), 7 deletions(-)

doc/langref.html.in+6-7
...@@ -3316,10 +3316,12 @@ test "default struct initialization fields" {...@@ -3316,10 +3316,12 @@ test "default struct initialization fields" {
3316 {#header_close#}3316 {#header_close#}
33173317
3318 {#header_open|extern struct#}3318 {#header_open|extern struct#}
3319 <p>An {#syntax#}extern struct{#endsyntax#} has in-memory layout guaranteed to match the3319 <p>An {#syntax#}extern struct{#endsyntax#} has in-memory layout matching
3320 C ABI for the target.</p>3320 the C ABI for the target.</p>
3321 <p>This kind of struct should only be used for compatibility with the C ABI. Every other3321 <p>If well-defined in-memory layout is not required, {#link|struct#} is a better choice
3322 use case should be solved with {#link|packed struct#} or normal {#link|struct#}.</p>3322 because it places fewer restrictions on the compiler.</p>
3323 <p>See {#link|packed struct#} for a struct that has the ABI of its backing integer,
3324 which can be useful for modeling flags.</p>
3323 {#see_also|extern union|extern enum#}3325 {#see_also|extern union|extern enum#}
3324 {#header_close#}3326 {#header_close#}
33253327
...@@ -3338,9 +3340,6 @@ test "default struct initialization fields" {...@@ -3338,9 +3340,6 @@ test "default struct initialization fields" {
3338 <li>An {#link|enum#} field uses exactly the bit width of its integer tag type.</li>3340 <li>An {#link|enum#} field uses exactly the bit width of its integer tag type.</li>
3339 <li>A {#link|packed union#} field uses exactly the bit width of the union field with3341 <li>A {#link|packed union#} field uses exactly the bit width of the union field with
3340 the largest bit width.</li>3342 the largest bit width.</li>
3341 <li>Non-ABI-aligned fields are packed into the smallest possible
3342 ABI-aligned integers in accordance with the target endianness.
3343 </li>
3344 </ul>3343 </ul>
3345 <p>3344 <p>
3346 This means that a {#syntax#}packed struct{#endsyntax#} can participate3345 This means that a {#syntax#}packed struct{#endsyntax#} can participate