#initialize an array with default zeroes?

1 messages · Page 1 of 1 (latest)

ionic ginkgo
#

test: [349]u1 = [1]u1{0} ** 349

#

if you dont like how that looks, you can also use std.mem.zeroes but then you have to repeat the type twice so its kind of annoying

#

yeah its creating an array consisting of one zero
and the ** basically duplicates that array
so i.e [1]u1{0} ** 3 would become a [3]u1{0, 0, 0}

#

yeah there’s also ++
its very useful

ionic ginkgo
#

@enumFromInt

#

and @intFromEnum

#

second in this case but youd probably want to know about both :p

#

then youll get a c_int back, should just work

#

oh sure

const index: usize = @intCast(@intFromEnum(enum_value));
void bluff
ionic ginkgo
#

fwiw, you may want to use std.EnumArray instead
itll handle mapping everything for you
and itll try to keep it as linear as possible (if the values are sparse itll make a mapping function)

#

fwiw its not actually a bit array here
u1 will be padded to a u8

#

depends on whats in it

#

itll only be padded to its natural alignment

#

fwiw you cant rely on the size of a struct unless its packed or extern
the compiler can do whatever
but you can assume itll be sane, so it will almost definitely only pad it if it has to

#

hm I dont think it has to be padded then?
if it’s 3 f32s, it’s natural alignment is 4 and 12 is already a multiple of 4

#

maybe it has something to do with SIMD needing specific alignment properties? I have no clue

#

since it seems to be using @Vector()

#

I dont think its that important to save bytes there personally