# Clarification about packed structs

**URL:** <https://ziggit.dev/t/clarification-about-packed-structs/1987>\
**Category:** Help\
**Tags:** language\
**Created:** [October 18, 2023, 10:20am UTC](https://ziggit.dev/t/clarification-about-packed-structs/1987 "2023-10-18T10:20:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![matklad](https://ziggit.dev/user_avatar/ziggit.dev/matklad/32/2462_2.png) [@matklad](https://ziggit.dev/u/matklad)\
**Post date:** [October 18, 2023, 10:20am UTC](https://ziggit.dev/t/clarification-about-packed-structs/1987/1 "2023-10-18T10:20:20Z")

</div>

The reference says:

[https://ziglang.org/documentation/master/#toc-packed-struct](https://ziglang.org/documentation/master/#toc-packed-struct)

> Unlike normal structs, `packed` structs have guaranteed in-memory layout:
> 
> - Fields remain in the order declared, least to most significant.
> - …
> - Non-ABI-aligned fields are packed into the smallest possible ABI-aligned integers in accordance with the target endianness.

I am having trouble parsing the meaning of the last one — all the words individually make sense to me, but the overall idea eludes me. Could someone give a couple of examples of what’s happening there?

---

<div class="post-metadata">

**Author:** ![andrewrk](https://ziggit.dev/user_avatar/ziggit.dev/andrewrk/32/7011_2.png) [@andrewrk](https://ziggit.dev/u/andrewrk)\
**Post date:** [October 19, 2023, 3:53am UTC](https://ziggit.dev/t/clarification-about-packed-structs/1987/2 "2023-10-19T03:53:57Z")

</div>

I’m having the exact same reaction as you. Let me explore the change that led to that wording…

OK I figured it out from `git blame`. It is a sentence that I added a long time ago when the compiler was written in C++ and packed structs worked completely differently. So, that sentence should simply be deleted.

Any other clarifications to this section while I’m at it?

---

<div class="post-metadata">

**Author:** ![matklad](https://ziggit.dev/user_avatar/ziggit.dev/matklad/32/2462_2.png) [@matklad](https://ziggit.dev/u/matklad)\
**Post date:** [October 19, 2023, 8:45am UTC](https://ziggit.dev/t/clarification-about-packed-structs/1987/3 "2023-10-19T08:45:09Z")

</div>

Nearby thing caught my eye (about extern structs)

> This kind of struct should only be used for compatibility with the C ABI. Every other use case should be solved with [packed struct](https://ziglang.org/documentation/master/#packed-struct) or normal [struct](https://ziglang.org/documentation/master/#struct).

When we were upgrading to 0.11

> <https://github.com/tigerbeetle/tigerbeetle/pull/1052/commits/ef3caa5e3461a7c7e56d18cd815234e74ccc163e>
>
> Should be using extern struct for defined layouts.

We changed a bunch of packed structs to extern structs, because we need defined memory layout, not an integer in a fancy hat.

Release changelog also shows example of using extern to control padding, so perhaps reference is a bit outdated?

> In the near future, the syntax will change to avoid confusion with C’s packed structs, which is a different concept that relates to padding. In Zig, such a struct is done by using `extern` and setting each field’s alignment to 1 byte:
