logoalt Hacker News

Memory layout in Zig with formulas

53 pointsby raymondtanatoday at 3:57 PM12 commentsview on HN

Comments

thechaotoday at 9:42 PM

I know this is a bit cursed; but, I always wanted a bitfield-on-steroids construct:

    struct Dang : bits 64    // 64 bits wide, int total
    {
        foo : bits 5 @ 0;    // 5 bits wide at bit offset 0
        bar : bits 5 @ 0;
        baz : bits 16 @ 4;   // 16 bits wide at bit offset 4
        tom : bits 11 @ 32;
    };
show 3 replies
raymondtanatoday at 3:57 PM

I've been learning Zig, and needed a refresher on memory layout (@sizeOf and @alignOf).

Wrote this blog post to summarize what I think are the right ways to understand alignment and size for various data types in Zig, just through experimentation.

Let me know any and all feedback!

show 1 reply
bk496today at 8:49 PM

useful!