# Defined memory layout separate from a C ABI

**URL:** <https://ziggit.dev/t/defined-memory-layout-separate-from-a-c-abi/2231>\
**Category:** Help\
**Tags:** language\
**Created:** [November 20, 2023, 5:11pm UTC](https://ziggit.dev/t/defined-memory-layout-separate-from-a-c-abi/2231 "2023-11-20T17:11:02Z")\
**Posts on this page:** 7\
**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:** [November 20, 2023, 5:11pm UTC](https://ziggit.dev/t/defined-memory-layout-separate-from-a-c-abi/2231/1 "2023-11-20T17:11:03Z")

</div>

The following code doesn’t work

```zig
17:07:47|~/tmp
λ bat main.zig 
pub const S = extern struct {
    x: u256,
};

pub fn f(s: S) void {
    _ = s;
}

pub fn main() void {
    f(.{ .x = 92 });
}

17:08:02|~/tmp
λ zig build-exe main.zig
main.zig:2:8: error: extern structs cannot contain fields of type 'u256'
    x: u256,
       ^~~~
main.zig:2:8: note: only integers with 0, 8, 16, 32, 64 and 128 bits are extern compatible

```

This makes sense C doesn’t support u256 yet, so there isn’t an ABI which could be used to pass this struct between C and Zig. But what if I don’t care about C ABI per se, and only need a well-defined memory layout (eg, I am describing the data as it exists on the disk / on the network)? Is there a way for me to make a bit-castable Zig struct with an u256 field and a well-defined memory layout, without, at the same time, guaranteeing a particular ABI for cross-language interop?

---

<div class="post-metadata">

**Author:** ![LucasSantos91](https://ziggit.dev/user_avatar/ziggit.dev/lucassantos91/32/440_2.png) [@LucasSantos91](https://ziggit.dev/u/LucasSantos91)\
**Post date:** [November 20, 2023, 6:37pm UTC](https://ziggit.dev/t/defined-memory-layout-separate-from-a-c-abi/2231/2 "2023-11-20T18:37:16Z")

</div>

You’re looking for `packed`.

---

<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:** [November 20, 2023, 6:51pm UTC](https://ziggit.dev/t/defined-memory-layout-separate-from-a-c-abi/2231/3 "2023-11-20T18:51:21Z")

</div>

I don’t think packed quite cuts it here: I don’t want my bools to turn into `u1`s, for example.

---

<div class="post-metadata">

**Author:** ![LucasSantos91](https://ziggit.dev/user_avatar/ziggit.dev/lucassantos91/32/440_2.png) [@LucasSantos91](https://ziggit.dev/u/LucasSantos91)\
**Post date:** [November 20, 2023, 7:37pm UTC](https://ziggit.dev/t/defined-memory-layout-separate-from-a-c-abi/2231/4 "2023-11-20T19:37:40Z")

</div>

Why would your `bool` turn into `u1`?

```zig
const std = @import("std");

pub const S = packed struct {
    _: u256 = 0,
    a: bool,
};

pub fn f(s: S) void {
    _ = s;
}

pub fn main() void {
    const s: S = .{.a = true};
    f(s);
    std.debug.print("{any}\n", .{s});
}

```

```zig
output: example.S{ ._ = 0, .a = true }

```

The bool is still a bool. If you’re saying that you don’t want the `bool` to occupy a single bit, just add whatever padding you need.

```zig
pub const S = packed struct {
    _: u256 = 0,
    a: bool,
    padding: u7 = undefined,
};

```

Now for external code, it will look as if the bool is occupying 8 bits.

---

<div class="post-metadata">

**Author:** ![slonik-az](https://ziggit.dev/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@slonik-az](https://ziggit.dev/u/slonik-az)\
**Post date:** [November 20, 2023, 7:58pm UTC](https://ziggit.dev/t/defined-memory-layout-separate-from-a-c-abi/2231/5 "2023-11-20T19:58:05Z")

</div>

> [@LucasSantos91](#):
>
> Now for external code, it will look as if the bool is occupying 8 bits.

But depending on your endianness it could be a 8-bit `bool` but with the wrong bit set.

---

<div class="post-metadata">

**Author:** ![LucasSantos91](https://ziggit.dev/user_avatar/ziggit.dev/lucassantos91/32/440_2.png) [@LucasSantos91](https://ziggit.dev/u/LucasSantos91)\
**Post date:** [November 20, 2023, 8:00pm UTC](https://ziggit.dev/t/defined-memory-layout-separate-from-a-c-abi/2231/6 "2023-11-20T20:00:50Z")

</div>

Yes, when working at such low level, you do need to take into consideration endianness. But the idea was to work with, for instance, a network protocol. The protocol would specify which bit would be the one you’re looking for, so you just define your struct so that the bool matches whatever the protocol specifies.

---

<div class="post-metadata">

**Author:** ![slonik-az](https://ziggit.dev/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@slonik-az](https://ziggit.dev/u/slonik-az)\
**Post date:** [November 20, 2023, 8:55pm UTC](https://ziggit.dev/t/defined-memory-layout-separate-from-a-c-abi/2231/7 "2023-11-20T20:55:03Z")

</div>

> [@LucasSantos91](#):
>
> Yes, when working at such low level, you do need to take into consideration endianness. But the idea was to work with, for instance, a network protocol.

Here comes the trouble. Speaking of network protocols, TCP/IP requires big-endian while most popular archs (x86, x86-64, aarch64) are little-endian. An explicit conversion is needed. Doing it for arbitrary packed struct is no fun.
