#What is the bool type in Zig, and why can't I write it in a struct to file?

1 messages · Page 1 of 1 (latest)

unreal oar
#

In C/C++ a bool is stored in a byte (8 bits) and a BOOL is stored in 4 bytes (32 bits). What is the memory layout for a bool in Zig, and why does attempting to write it out using std.fs.File.writer().writeStruct() result in the following error?

lib/std/mem.zig:2023:57: error: expected integer or vector, found 'bool'
                        @field(ptr, f.name) = @byteSwap(@field(ptr, f.name));
                                                        ^~~~~~~~~~~~~~~~~~~
referenced by:
    writeStructEndian__anon_3446: /home/aero/Utility/zig-linux-x86_64-0.13.0/lib/std/io/Writer.zig:69:30
    writeStructEndian: /home/aero/Utility/zig-linux-x86_64-0.13.0/lib/std/io.zig:348:59
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

Here is the struct I was attempting to test writing with:

const BoxedTypeOne = packed struct {
    flag: bool,
    letter: u8,
    u32_value: u32,
};
rugged coral
#

A bool is one byte big, but in a packed struct, it is one bit big.

#

Also, if you are coming from C/C++: Think of a packed struct more as a bitfield. If you want to something like a C/C++ packed struct, create an (extern) struct and align all fields to one byte.

soft ermine