# writeStructEndian writes padding bytes of packed structs due to alignment

**URL:** <https://ziggit.dev/t/writestructendian-writes-padding-bytes-of-packed-structs-due-to-alignment/5271>\
**Category:** Help\
**Tags:** standard-library\
**Created:** [July 21, 2024, 7:06am UTC](https://ziggit.dev/t/writestructendian-writes-padding-bytes-of-packed-structs-due-to-alignment/5271 "2024-07-21T07:06:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [July 21, 2024, 7:06am UTC](https://ziggit.dev/t/writestructendian-writes-padding-bytes-of-packed-structs-due-to-alignment/5271/1 "2024-07-21T07:06:58Z")

</div>

Am I using writeStructEndian wrong or is this a bug?

```zig
test "header write" {
    const EthernetHeader = packed struct(u112) {
        dest_mac: u48,
        src_mac: u48,
        ether_type: u16,
    };

    const header: EthernetHeader = .{
        .dest_mac = 0xAABB_CCDD_EEFF,
        .src_mac = 0x1122_3344_5566,
        .ether_type = 0x88a4,
    };

    var buf: [64]u8 = std.mem.zeroes([64]u8);
    var fbs = std.io.fixedBufferStream(&buf);
    var writer = fbs.writer();
    try writer.writeStructEndian(header, std.builtin.Endian.big);
    std.log.warn("wrote: {x}", .{fbs.getWritten()});
    // prints: { aa, bb, cc, dd, ee, ff, 11, 22, 33, 44, 55, 66, 88, a4, 0, 0 }
    // the 0, 0 at the end should not be there! ^ ^ bad!!
    try std.testing.expect(fbs.getWritten().len == 112 / 8);
}

```

---

<div class="post-metadata">

**Author:** ![IntegratedQuantum](https://ziggit.dev/user_avatar/ziggit.dev/integratedquantum/32/782_2.png) [@IntegratedQuantum](https://ziggit.dev/u/IntegratedQuantum)\
**Post date:** [July 21, 2024, 7:18am UTC](https://ziggit.dev/t/writestructendian-writes-padding-bytes-of-packed-structs-due-to-alignment/5271/2 "2024-07-21T07:18:26Z")

</div>

The problem here is that the compiler adds alignment to integer types, which in this case adds padding zeroes. For example `u112` is aligned to 16 bytes which makes  
`@sizeOf(u112) == 16 == @sizeOf(u128)`

I think you can do the byteSwap manually and then use `.writeInt(u112, @bitCast(byteSwappedStruct), .little);`

---

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [July 21, 2024, 7:20am UTC](https://ziggit.dev/t/writestructendian-writes-padding-bytes-of-packed-structs-due-to-alignment/5271/3 "2024-07-21T07:20:40Z")

</div>

yeah… this assertion fails, so i think you are right about the padding zeros

```zig
const EthernetHeader = packed struct(u112) {
        dest_mac: u48,
        src_mac: u48,
        ether_type: u16,

        comptime {
            std.debug.assert(@sizeOf(@This()) == 112 / 8);
        }
    };

```

---

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [July 21, 2024, 7:22am UTC](https://ziggit.dev/t/writestructendian-writes-padding-bytes-of-packed-structs-due-to-alignment/5271/4 "2024-07-21T07:22:16Z")

</div>

this makes me really question if write struct endian should even exist, it already required packed structs:

```zig
pub fn writeStruct(self: Self, value: anytype) anyerror!void {
    // Only extern and packed structs have defined in-memory layout.
    comptime assert(@typeInfo(@TypeOf(value)).Struct.layout != .auto);
    return self.writeAll(mem.asBytes(&value));
}

```

---

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [July 21, 2024, 7:24am UTC](https://ziggit.dev/t/writestructendian-writes-padding-bytes-of-packed-structs-due-to-alignment/5271/5 "2024-07-21T07:24:33Z")

</div>

or perhaps writeStructEndian should be changed to not add the padding of packed structs?
