Skip to content

Instantly share code, notes, and snippets.

@daurnimator
Last active May 11, 2019 08:05
Show Gist options
  • Save daurnimator/81264b23c0961909cbc56371a8166901 to your computer and use it in GitHub Desktop.
Save daurnimator/81264b23c0961909cbc56371a8166901 to your computer and use it in GitHub Desktop.
const std = @import("std");
const some_cond = false;
const My_Vla_Struct = struct {
x: if (some_cond) i32 else void,
const Self = @This();
pub fn usefulMethod(self: *Self) void {
// ...
}
// object is followed by variable length array
};
const ListType = std.LinkedList(My_Vla_Struct);
fn newNode(allocator: *std.mem.Allocator, extra_size: usize) !*ListType.Node {
const node_ptr = try allocator.alignedAlloc(u8, @alignOf(ListType.Node), @sizeOf(ListType.Node) + extra_size);
return @ptrCast(*ListType.Node, node_ptr.ptr);
}
test "vla" {
const node = try newNode(std.debug.global_allocator, 10);
// const vla_ptr = @ptrCast([*]u8, &node.data); // zig: /build/zig/src/zig-0.4.0/src/codegen.cpp:6083: LLVMOpaqueValue* gen_const_val(CodeGen*, ConstExprValue*, const char*): Assertion `type_has_bits(type_entry)' failed.
// const vla_ptr = @ptrToInt(&node.data); // error: pointer to size 0 type has no address
const data = &node.data;
data.usefulMethod();
// const vla_ptr = @ptrToInt(data); // error: pointer to size 0 type has no address
// const vla_ptr = @ptrCast([*]u8, data); // unreachable: /build/zig/src/zig-0.4.0/src/codegen.cpp:gen_const_val_ptr:5956
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment