#Struct member access?

1 messages · Page 1 of 1 (latest)

pseudo grail
#

in the first function you're reading the bytes that come after the header field; in the second function you're reading the bytes that the tables field points to.
both functions are quite hard to understand - can you explain what you're trying to do, and where do the data come from?

#
findTable
+--------+--------+
| header | tables |
+--------+--------+
         ~~~~~~~~~~~~~~~ reading this

findTable1
+--------+--------+
| header | tables |
+--------+--------+
             |
             |
             |      +-------------+
             +----> | ... ... ... |
                    +-------------+
                    ~~~~~~~~~~~~~~~ reading this
#

if findTable (no 1) is the working version - you have done your type-modelling wrong.

#

"type modelling" as in, writing Zig types that represent the meaning of some sequence of bytes, according to some format

#

the tables field in the first code snippet is wrong - it represents a pointer to some remote data; when you actually want to represent a sequence of inline elements, in the classic header-body format

#

Zig does not have flexible array members, I suggest you completely remove the tables field and instead make a utility method:

const LengthPrefixedString = extern struct {
    len: u64,
    body: void,

    fn getSlice(self: *const LengthPrefixedString) []const u8 {
        const ptr: [*]const u8 = @ptrCast(&self.body);
        return ptr[0..self.len];
    }
};
memory representation:
+--------+----+----+----+-/ /-+----+
| length | u8 | u8 | u8 | \ \ | u8 |
+--------+----+----+----+-/ /-+----+
         |-------------------------| length given by the len field