Created
December 14, 2025 09:14
-
-
Save ninyawee/ed21b89b94c145dedfc55dde43f19e61 to your computer and use it in GitHub Desktop.
zig is low level in python. trust in human. the dangling pointer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [tools] | |
| zig = "latest" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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