Zig

From G1Joshi/Agent-Skills · View interactive version

Zig is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Zig systems programming assistance covering comptime code execution, manual memory allocators, C interop, and cross-compilation. Use when writing low-level systems software, embedded apps, or high-performance libraries.

How to use this skill

Skill file

Zig

Zig is a modern systems programming language offering manual memory management, compile-time code execution (comptime), and a built-in cross-compiling C/C++ toolchain.

When to Use

Quick Start

const std = @import("std");

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("Hello from Zig!
", .{});
}

Core Concepts

Explicit Memory Allocation with GeneralPurposeAllocator

Zero hidden allocations—every data structure requires an explicit allocator:

const std = @import("std");

pub fn main() !void {
    // Standard debug memory allocator with leak detection
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer {
        const check = gpa.deinit();
        if (check == .leak) std.debug.print("Memory leak detected!
", .{});
    }
    const allocator = gpa.allocator();

    // Dynamically allocated array list
    var list = std.ArrayList(u32).init(allocator);
    defer list.deinit();

    try list.append(10);
    try list.append(20);
    try list.append(30);

    for (list.items) |item| {
        std.debug.print("Item: {d}
", .{item});
    }
}

Comptime Generics & Metaprogramming

Compile-time code execution replacing C++ templates and C macros:

const std = @import("std");

// Generic Queue data structure generated at compile-time
pub fn Queue(comptime T: type) type {
    return struct {
        const Self = @this();
        items: std.ArrayList(T),

        pub fn init(allocator: std.mem.Allocator) Self {
            return Self{
                .items = std.ArrayList(T).init(allocator),
            };
        }

        pub fn deinit(self: *Self) void {
            self.items.deinit();
        }

        pub fn push(self: *Self, value: T) !void {
            try self.items.append(value);
        }

        pub fn pop(self: *Self) ?T {
            if (self.items.items.len == 0) return null;
            return self.items.orderedRemove(0);
        }
    };
}

Error Handling, Defer & Errdefer

Deterministic resource management and lightweight error unions:

const std = @import("std");

const FileError = error{
    AccessDenied,
    FileNotFound,
    DiskFull,
};

fn openAndProcessFile(allocator: std.mem.Allocator, filename: []const u8) !void {
    const buffer = try allocator.alloc(u8, 1024);
    // defer runs unconditionally when exiting scope
    defer allocator.free(buffer);

    var file = std.fs.cwd().openFile(filename, .{}) catch |err| switch (err) {
        error.FileNotFound => return FileError.FileNotFound,
        error.AccessDenied => return FileError.AccessDenied,
        else => return error.DiskFull,
    };
    defer file.close();

    // errdefer only runs if an error is returned later in the function
    errdefer std.debug.print("Error occurred while reading file: {s}
", .{filename});

    _ = try file.readAll(buffer);
}

Common Patterns

Explicit Memory Allocation with Defer Cleanup

Problem: Hidden allocations in language runtimes make memory leaks difficult to identify and fix.

Solution:

Pass explicit allocator parameter and defer cleanup immediately:

const std = @import("std");

pub fn createBuffer(allocator: std.mem.Allocator, size: usize) ![]u8 {
    const buf = try allocator.alloc(u8, size);
    errdefer allocator.free(buf);

    @memset(buf, 0);
    return buf;
}

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    const buffer = try createBuffer(allocator, 1024);
    defer allocator.free(buffer);
}

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :---------------------------------------- | :--------------------------------------------------- | :-------------------------------------------------------------------- |

| error: memory leak detected | GPA allocator detected memory not freed at shutdown. | Ensure every allocator.alloc has a matching defer allocator.free. |

| error: use of undeclared identifier | Symbol misspelled or missing @import. | Check spelling and verify imported namespace. |

| error: expected type '...', found '...' | Static type mismatch in expression. | Use explicit cast @as(TargetType, val) or @intCast. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.