How to declare a zeroed array without typing all the elements?

Hi,
I want to declare an array of lengh 1024. I will use it as an poors-man stack implementation for a little bare-metal program.

How can I declared the zeroed array without typing all the elements?

const stack: [1024]u8 = ???

Thx in advance,

Alvaro

const stack = [_]u8 {0} ** 1024; should do the trick.

Edit: Now I also found the snippet from the official docs:

// initialize an array to zero
const all_zero = [_]u16{0} ** 10;
8 Likes
var array: [1024]u8 = undefined;
@memset(&array, 0);

Is another option if you find the double asterics syntax hard to read

2 Likes

I don’t typically see this, but I’ve taken a liking to

var stack = std.mem.zeroes([1024]u8);
4 Likes

Thx, for the help!

1 Like