Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

5
random.float causes segfault in unrelated code?
Post Body

It's me again. Sorry for the double post.

zig test filename.zig on the following code produces a segfault on the second test.

const std = @import("std");
const allocator = std.heap.page_allocator;
var random = std.rand.DefaultPrng.init(142857).random;

fn alloc(comptime T: type, sz: usize) []T {
    const mem = allocator.alloc(T, sz) catch unreachable;
    return mem;
}
fn free(address: anytype) void {
    allocator.free(address);
}

test "no segfault without random.float" {
    // const x1 = random.float(f64);
    var thing = alloc(f64, 8);
    free(thing);
}

test "no segfault without alloc and free" {
    const x1 = random.float(f64);
    // var thing = alloc(f64, 8);
    // free(thing);
}

test "segfault when used together" {
    const x1 = random.float(f64);
    var thing = alloc(f64, 8);
    free(thing);
}

Here is the output:

$ zig test filename.zig 
Test [3/3] test "segfault when used together"... Segmentation fault at address 0x0
error: the following test command crashed:
zig-cache/o/43f68cfee589a095c17e125098f6a93c/test

It almost surely has to do with the ugly alloc() and free() things I made - I know some C but I'm by no means an expert, and was looking for some familiarity. The segfault dissappears if I call allocator.alloc or allocator.free directly.

I am however baffled at the fact that the x1 line interferes with the rest.

What is even happening here?

Author
Account Strength
100%
Account Age
9 years
Verified Email
Yes
Verified Flair
No
Total Karma
13,881
Link Karma
1,039
Comment Karma
12,809
Profile updated: 5 hours ago
Posts updated: 7 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
4 years ago