undefined seems perfectly safe for most types, but []T pointers cause segfaults when indexed.
pub fn main() void {
comptime {
var arr: []u8 = undefined;
arr[0] = 2;
}
}
sapphire@Raphi-Spoerri:~$ zig build-exe main.zig
Segmentation fault (core dumped)
md5-8041aeec57bc8d66c9d9655022727c73
```Bash
sapphire@Raphi-Spoerri:~$ zig build-exe main.zig
sapphire@Raphi-Spoerri:~$ ./main
Segmentation fault at address 0x0
/home/sapphire/main.zig:3:14: 0x22a866 in main (main)
arr[0] = 2;
^
/usr/lib/zig/0.6.0/lib/zig/std/start.zig:243:22: 0x2046df in std.start.posixCallMainAndExit (main)
root.main();
^
/usr/lib/zig/0.6.0/lib/zig/std/start.zig:123:5: 0x2044bf in std.start._start (main)
@call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
^
Aborted (core dumped)
To quote the documentation:
undefined means the value could be anything, even something that is nonsense according to the type. Translated into English, undefined means "Not a meaningful value. Using this value would be a bug. The value will be unused, or overwritten before being used."
Also, a []u8 is a slice not an array. An array would be for example [42]u8. You can think of a slice as a struct:
const LikeASlice = struct {
ptr: [*]u8,
len: usize,
}
So, when you initialize a slice to undefined and then dereference the pointer by accessing the first element you are accessing some unknown and likely invalid memory address, causing a segfault.
But I think it should be checked in comptime (because it has been placed in comptime block)
But I think it should be checked in comptime (because it has been placed in comptime block)
Certainly.
Moreover, it should throw a more helpful error at runtime, something like attempting to dereference an undefined value, rather than Segmentation fault at address 0x0.
@RaphiJS6-13
attempting to dereference an undefined value
I think it is no possible... or your debug build may 10x larger and slower...
@RaphiJS6-13
attempting to dereference an undefined value
I think it is no possible... or your debug build may 10x larger and slower...
Why? How is it any different than checking the array bounds?
@RaphiJS6-13
attempting to dereference an undefined value
I think it is no possible... or your debug build may 10x larger and slower...
Why? How is it any different than checking the array bounds?
Because no valid value for undefined...
Consider you put undefined in a struct field, there no more room for storing the this value is undefined information...
@RaphiJS6-13
attempting to dereference an undefined value
I think it is no possible... or your debug build may 10x larger and slower...
Why? How is it any different than checking the array bounds?
Because no valid value for undefined...
Consider you put undefined in a struct field, there no more room for storing thethis value is undefinedinformation...
Really? If undefined is just uninitialized data, then why is it impossible to assign one field to undefined and leave the other fields unchanged?
Most helpful comment
But I think it should be checked in comptime (because it has been placed in comptime block)