Skip to content

Commit

Permalink
更新packed struct说明 (#185)
Browse files Browse the repository at this point in the history
* 更新packed struct说明

* 增加额外消息来源

* Update course/basic/advanced_type/struct.md

Co-authored-by: Haoyu Qiu <[email protected]>

---------

Co-authored-by: Haoyu Qiu <[email protected]>
  • Loading branch information
jinzhongjia and timothyqiu authored Sep 5, 2024
1 parent a70d6ae commit ea9ffd5
Showing 1 changed file with 2 additions and 8 deletions.
10 changes: 2 additions & 8 deletions course/basic/advanced_type/struct.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ zig 在使用结构体的时候还支持省略结构体类型,只要能让 zig

:::

4. 默认情况下 zig 还会对字段进行重新排序,但是在 packed 下并不会重新排序。
4. `packed struct` 会保证字段的顺序以及在字段间不存在额外的填充,但针对结构体本身可能仍然存在额外的填充:

:::details 示例

Expand All @@ -224,13 +224,7 @@ zig 在使用结构体的时候还支持省略结构体类型,只要能让 zig
| 4(i32) | 8(pointer) | 4(padding) |
```

如果去掉 packed,则是

```sh
| 8(pointer | 4(int32) | 4(padding) |
```
可以看到, Zig 会对字段进行重新排序。
额外的讨论信息:[github issue #20265](https://github.com/ziglang/zig/issues/20265)

:::

Expand Down

0 comments on commit ea9ffd5

Please sign in to comment.