`std.debug.print` ignore undefined value

const std = @import("std");

pub const A = struct {
    a: *u32,
};

pub fn main() !void {
    const value: A = undefined;
    std.debug.print("{any}\n", .{ value });
}

Output

.{ .a = u32@00 }

Is this the right behavior?

That’s the format of a pointer address - It’s printing the address of the *u32 pointer. If you want to print the value, do value.a.*. But, to my knowledge, the {any} format string never automatically prints derefereced pointer values.

No, my question is, shouldn’t std.debug.print handle the undefined value? value is a undefined value here, but it has been ignored.

undefined in zig means that the value in that piece of memory can be anything and is not explicitly initialized by code yet, it’s different from the concept of undefined in e.g. Javascript

For example, in debug builds, undefined could set the memory to 0xAAAAA... (or anything else really) to help with debugging, but in optimized builds undefined generates no code and value in the variable will just be whatever was already contained there in memory until you explicitly write a non-undefined value into it.

4 Likes