Skip to content

Instantly share code, notes, and snippets.

@lithdew
Created July 30, 2020 10:05
Show Gist options
  • Save lithdew/d01ab5beec771c8e51c29c66465842e8 to your computer and use it in GitHub Desktop.
Save lithdew/d01ab5beec771c8e51c29c66465842e8 to your computer and use it in GitHub Desktop.
zig: stack alloc recursive data structure
const std = @import("std");
const Node = struct {
left: ?*Node = null,
right: ?*Node = null,
pub fn new(allocator: *std.mem.Allocator) !Node {
return Node{
.left = try allocator.create(Node),
.right = try allocator.create(Node),
};
}
};
pub fn main() !void {
var buf: [65536]u8 = undefined;
var buffer = std.heap.FixedBufferAllocator.init(&buf);
var node = try Node.new(&buffer.allocator);
std.debug.print("{}\n", .{node});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment