Skip to content

Commit 6d0f3da

Browse files
committed
feat: duplicate_buffer example
1 parent 92a01d4 commit 6d0f3da

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/duplicate_buffer.zig

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const std = @import("std");
2+
3+
fn printMap(map: *const [][]u8) void {
4+
std.debug.print("\n", .{});
5+
for (0..map.len) |x| {
6+
const row = map.*[x];
7+
std.debug.print("{d: >3} ", .{x});
8+
for (0..row.len) |y| {
9+
std.debug.print("{c}", .{map.*[x][y]});
10+
}
11+
std.debug.print("\n", .{});
12+
}
13+
}
14+
15+
fn fillMap(map: *const [][]u8) void {
16+
for (0..map.len) |x| {
17+
const row = map.*[x];
18+
for (0..row.len) |y| {
19+
map.*[x][y] = '#';
20+
}
21+
}
22+
}
23+
24+
pub fn main() !void {
25+
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
26+
defer arena.deinit();
27+
const allocator = arena.allocator();
28+
29+
var map = try allocator.alloc([]u8, 10);
30+
for (0..map.len) |x| {
31+
map[x] = try allocator.alloc(u8, 12);
32+
for (0..map[x].len) |y| {
33+
map[x][y] = '.';
34+
}
35+
}
36+
printMap(&map);
37+
38+
const map_copy: [][]u8 = try allocator.alloc([]u8, map.len);
39+
for (0..map.len) |x| {
40+
map_copy[x] = try allocator.alloc(u8, map[x].len);
41+
for (0..map[x].len) |y| map_copy[x][y] = map[x][y];
42+
}
43+
44+
std.debug.print("\nMap:", .{});
45+
printMap(&map);
46+
std.debug.print("\nCopy:", .{});
47+
printMap(&map_copy);
48+
std.debug.print("\nAfter Fill:", .{});
49+
fillMap(&map);
50+
printMap(&map);
51+
printMap(&map_copy);
52+
}

0 commit comments

Comments
 (0)