Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sentinel-terminated array = undefined makes sentinel uninitialized #19930

Open
Jonatanc05 opened this issue May 10, 2024 · 3 comments
Open

Sentinel-terminated array = undefined makes sentinel uninitialized #19930

Jonatanc05 opened this issue May 10, 2024 · 3 comments
Labels
Milestone

Comments

@Jonatanc05
Copy link

Zig Version

0.12.0-dev.3533+e5d900268

Steps to Reproduce and Observed Behavior

I'm trying to use [32:0]u8 to interop with C. First, I'll show the code that worked for me:

    var sha256_1: [33]u8 = undefined;
    sha256_1[32] = 0x00;
    @breakpoint();

When the breakpoint is hit, memory at the address &sha256_1 obviously looks right:

aa aa aa aa aa aa aa aa  ªªªªªªªª
aa aa aa aa aa aa aa aa  ªªªªªªªª
aa aa aa aa aa aa aa aa  ªªªªªªªª
aa aa aa aa aa aa aa aa  ªªªªªªªª
00                       .

Now the code that I thought should work the same:

    var sha256_1: [32:0]u8 = undefined;
    @breakpoint();

but now memory is

aa aa aa aa aa aa aa aa  ªªªªªªªª
aa aa aa aa aa aa aa aa  ªªªªªªªª
aa aa aa aa aa aa aa aa  ªªªªªªªª
aa aa aa aa aa aa aa aa  ªªªªªªªª
aa                       ª

I'm on windows 11 btw

Expected Behavior

I would expect the sentinel to be initialized, just like I did manually in the first snippet. My intuition is that = undefined would not affect the sentinel. This might be a design decision but I haven't found any issue about it. I understand (guess) there might be efficiency reasons to this behaviour, maybe futurely we can include this in docs.

@Jonatanc05 Jonatanc05 added the bug Observed behavior contradicts documented or intended behavior label May 10, 2024
@Vexu Vexu added docs and removed bug Observed behavior contradicts documented or intended behavior labels May 10, 2024
@Vexu Vexu added this to the 0.15.0 milestone May 10, 2024
@MatthiasPortzel

This comment was marked as outdated.

@silversquirl
Copy link
Contributor

Initializing an array with this type of pattern is very common, and should not require manually setting the sentinel:

var array: [n:0]u32 = undefined;
for (&array, 0..) |*x, i| {
    x.* = @intCast(i);
}

@MatthiasPortzel
Copy link

MatthiasPortzel commented May 16, 2024

I still wish the semantics in my comment were correct, but I’ve been informed that I was mistaken about how sentinel-terminal-arrays actually work. (In particular, I expected operations on the array, like @silversquirl’s example, to use the sentinel to determine the length, when the compile-time length is used instead.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants