#uninitialized variables

1 messages · Page 1 of 1 (latest)

stuck anchor
#

when I crate uninitialized variable with undefined, it gets filled with character 170. is it the default one? if so, why 170 instead of zero?

#

additionally, should I use char 170 when I want to mem.set reset the area?

round sundial
#

In debug mode, Zig will write 0xaa to the memory region of the undefined variable. This makes it easy to detect uninitialised memory during crashes 🙂

round sundial
stuck anchor
#

hmm, when I create the executable with zig build -Drelease-safe it still uses 170 though

stuck anchor
round sundial
#

Apologies, I meant in safe modes

ashen pilot
# stuck anchor that's actually what I do but I want to reuse the same memory area, `mem.set`ing...

yeah, if you just want to clear the buffer to sort of "mark" it for re-use undefined is probably better (don't explicitly write 0xaa/170, just use undefined - it's clearer and also makes the operation a nop in release-fast). the reason that's done is essentially that 0x00 is a really common byte all over the place and its presence doesn't tell you much, whereas 0xaa is a much less common pattern which should therefore immediately make you think "uninitialized memory" when debugging zig

magic agate
#

As for why 0xaa specifically, it has an alternating bit pattern

#

So in binary it's 10101010

#

Which makes it very obvious in binary, octal, and hex

#

It's less obvious in decimal but it's still recognizable