$ 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