Closed Traxar closed 1 month ago
Here's a related example:
const std = @import("std");
const S = packed struct {
a: usize = undefined,
b: usize,
};
pub fn main() void {
const s_const: S = .{ .b = 123 };
std.debug.print("{}\n", .{s_const.b});
var s_var: S = .{ .b = 123 };
_ = &s_var;
std.debug.print("{}\n", .{s_var.b});
}
As of 0.14.0-dev.1349+6a21875dd, this prints
123
17007316
(at least for me; the second value will not necessarily be consistent since it appears to be uninitialized memory: https://godbolt.org/z/PM85achM7)
This example works as expected (prints 123 twice) if any of the following changes are made:
S
a regular (non-packed) structb
before a
a
besides undefined
May be related to #20095 and #20938 (both of which involve undefined
being handled incorrectly in packed structs, but with different outcomes)
// var s_var: S = .{ .b = 123 };
var s_var: S = undefined;
s_var.b = 123;
This does not change anything about the behaviour elsewhere, compared to the other workarounds
Duplicate of #20938
Zig Version
0.13.0
Steps to Reproduce and Observed Behavior
Expected Behavior
test passes