Skip to content

Instantly share code, notes, and snippets.

@50n1cd347h9
Last active September 2, 2024 17:09
Show Gist options
  • Save 50n1cd347h9/23dc7421876cf1c9e1f8b38ade2b6eaa to your computer and use it in GitHub Desktop.
Save 50n1cd347h9/23dc7421876cf1c9e1f8b38ade2b6eaa to your computer and use it in GitHub Desktop.
$ zig version
0.14.0-dev.1392+37df6ba86
const std = @import("std");
const fmt = std.fmt;
const debugPrint = std.debug.print;

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();

pub fn main() !void {
    var hoge = try allocator.alloc(u8, 0x10);
    _ = try fmt.bufPrint(hoge, "aaa", .{});

    debugPrint("/{s}/\n", .{hoge});
    debugPrint("hoge {p} -> {p}\n", .{ &hoge, &hoge[0] });

    const ahi = try allocator.alloc(u8, 0x10);
    _ = try fmt.bufPrint(ahi, "bbb", .{});

    debugPrint("/{s}/\n", .{ahi});
    debugPrint("ahi  {p} -> {p}\n", .{ &ahi, &ahi[0] });

    allocator.free(hoge);
    hoge = ahi;

    debugPrint("/{s}/\n", .{hoge});
    debugPrint("hoge {p} -> {p}\n", .{ &hoge, &hoge[0] });
    debugPrint("ahi  {p} -> {p}\n", .{ &ahi, &ahi[0] });

    allocator.free(ahi);
}
$ ./memswap
/aaa/
hoge []u8@7fff1ca61370 -> u8@7dcc9e927000
/bbb/
ahi  []u8@7fff1ca613e8 -> u8@7dcc9e927010
/bbb/
hoge []u8@7fff1ca61370 -> u8@7dcc9e927010
ahi  []u8@7fff1ca613e8 -> u8@7dcc9e927010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment