News for hackers. Stuff that matters.
Re:Zig, the Small Language by ptato on Wednesday September 07, 2022 @01:34PM (#32753571)
It's really 4. It doesn't count the sentinel, just the elements. `@sizeOf([4:0]u8)` is 5 though.
Re:Zig, the Small Language by Graziano_M on Wednesday September 07, 2022 @01:47PM (#32753754)
Oh, that's strange. So you can actually read (just 1) past len?
Re:Zig, the Small Language by sl3dge78 on Wednesday September 07, 2022 @02:18PM (#32754167)
Yeah, it's made to feel like strlen in a way. Sentinel types are mostly used with C code. That way strlen(some_string) == some_string.len.
Re:Zig, the Small Language by Graziano_M on Wednesday September 07, 2022 @04:07PM (#32756028)
Ah, I hadn't thought of that analogue, but it makes sense, then. It sort of recreates the opportunity to make the common bug, though, of allocating enough for the data but not the sentinel (or enough for the string but not the null terminator).
Re:Zig, the Small Language by kristoff_it on Wednesday September 07, 2022 @04:16PM (#32756165)
Not really, the sentinel is part of the type and the allocator interface in Zig is type-aware, meaning that you can't simply "forget" about sentinels. The type system will fight you.
Re:Zig, the Small Language by Graziano_M on Thursday September 08, 2022 @11:01AM (#32766604)
So creating a pointer to a type with a fixed length and a sentinel and creating a new one will automatically allocate enough for the length + the sentinel? That's nice.

I'm still not sure I like the .len 'misreporting' since it would be harder to manually optimize around, e.g. packing a struct with a 8 byte + sentinel array would take 16 bytes that I might not notice if I am going by `.len`. This is all speculation, though. I need to actually play around with Zig.