Skip to content

segmentation fault with mem.Allocator = undefined #2495

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

Closed
komuw opened this issue May 15, 2019 · 1 comment
Closed

segmentation fault with mem.Allocator = undefined #2495

komuw opened this issue May 15, 2019 · 1 comment

Comments

@komuw
Copy link

komuw commented May 15, 2019

uname -ra:
Darwin Kernel Version root:xnu-3789.73.27~1/RELEASE_X86_64 x86_64

zig version:
0.4.0+057a5d48

I wanted to know how allocators work so I created the following program;

const std = @import("std");
const warn = std.debug.warn;
const mem = std.mem;

// Taken from; https://github.com/ziglang/zig/blob/057a5d4898f70c6a8169c99375fbb8631e539051/test/standalone/brace_expansion/main.zig#L19
var global_allocator: *mem.Allocator = undefined;

pub fn main() !void {
    var list = std.ArrayList(i32).init(global_allocator);
    defer list.deinit();

    try list.append(1);
    // warn("list: {}", list);
}

zig fmt . && zig build-exe main.zig && ./main

[1]    26176 segmentation fault  ./main

I do not know if var global_allocator: *mem.Allocator = undefined; should work, but even if it shouldn't; I would expect a compile error instead of segfault

@andrewrk
Copy link
Member

There is no plan to make this a compile error. How would the compiler know the global variable was undefined? It could have been changed anywhere.

On the other hand it is planned to have the runtime crash from branching on an undefined value easier to debug. #63

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

No branches or pull requests

2 participants