Skip to content

Commit 5dc7f5c

Browse files
committed
Add warning for #[repr(C)] struct alignment pseudocode
1 parent bc5c540 commit 5dc7f5c

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/type-layout.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ for field in struct.fields_in_declaration_order() {
226226
struct.size = current_offset + padding_needed_for(current_offset, struct.alignment);
227227
```
228228

229+
<div class="warning">
230+
231+
Warning: The function used to calculate the needed padding in this pseudocode is
232+
a naive implementation that doesn't account for overflow issues. For a rigorous
233+
implementation suitable for performing this calculation in actual code, see the
234+
source code for [`Layout::padding_needed_for`].
235+
236+
</div>
237+
229238
> Note: This algorithm can produce zero-sized structs. This differs from
230239
> C where structs without data still have a size of one byte.
231240
@@ -374,3 +383,4 @@ used with any other representation.
374383
[`C`]: #the-c-representation
375384
[primitive representations]: #primitive-representations
376385
[`transparent`]: #the-transparent-representation
386+
[`Layout::padding_needed_for`]: ../std/alloc/struct.Layout.html#method.padding_needed_for

0 commit comments

Comments
 (0)