Skip to content

Instantly share code, notes, and snippets.

@ninyawee
Created December 14, 2025 09:14
Show Gist options
  • Select an option

  • Save ninyawee/ed21b89b94c145dedfc55dde43f19e61 to your computer and use it in GitHub Desktop.

Select an option

Save ninyawee/ed21b89b94c145dedfc55dde43f19e61 to your computer and use it in GitHub Desktop.
zig is low level in python. trust in human. the dangling pointer.
[tools]
zig = "latest"
const std = @import("std");
const print = std.debug.print;
pub fn ub() *u16 {
var x: u16 = 69;
x = 69;
return &x; // Returning pointer to stack variable
}
pub fn ud() *u16 {
var x: u16 = 420;
x = 420;
return &x; // Same problem
}
pub fn main() void {
const a = ub();
print("The integer is: {}\n", .{a.*}); // 69 - stack not reused yet
_ = ud();
print("The integer is: {}\n", .{a.*}); // 420 - ud() clobbered a's memory!
const b = ud();
print("The integer is: {}\n", .{b.*}); // 420
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment