aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/exercises/076a_memory_allocation.zig
diff options
context:
space:
mode:
authorSean Aubin <sean@proteinqure.com>2023-02-28 20:54:42 -0500
committerSean Aubin <sean@proteinqure.com>2023-03-08 13:35:02 -0500
commit1be1d854a8f690c25a577bef1feb1e9926caf2c7 (patch)
tree0a006ab9969a699f283798eec22e833b8906d277 /exercises/076a_memory_allocation.zig
parentf10f9efe7f092a662a529ee59e25c75cb60a90be (diff)
first draft of memory allocation exercise
Diffstat (limited to 'exercises/076a_memory_allocation.zig')
-rw-r--r--exercises/076a_memory_allocation.zig70
1 files changed, 70 insertions, 0 deletions
diff --git a/exercises/076a_memory_allocation.zig b/exercises/076a_memory_allocation.zig
new file mode 100644
index 0000000..b098773
--- /dev/null
+++ b/exercises/076a_memory_allocation.zig
@@ -0,0 +1,70 @@
+// In most of the examples so far, the inputs are known at compile time, thus
+// the amount of memory used by the program is fixed and is requested. However, if responding to
+// input whose size is not known at compile time, such as:
+// - user input via command-line arguments
+// - inputs from another program
+//
+// You'll need to request memory for you program to be allocated by your
+// operating system at runtime.
+//
+// Zig provides several different allocators. In the Zig documentation, it
+// recommends the Arena allocator for simple programs which allocate once and
+// then exit:
+//
+// const std = @import("std");
+//
+// // memory allocation can fail because your computer is out of memory, so
+// // the return type is !void
+// pub fn main() !void {
+//
+// var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
+// defer arena.deinit();
+//
+// const allocator = arena.allocator();
+//
+// const ptr = try allocator.create(i32);
+// std.debug.print("ptr={*}\n", .{ptr});
+//
+// const slice_ptr = try allocator.create(i32);
+// std.debug.print("ptr={*}\n", .{ptr});
+// }
+
+// Instead of a simple integer, this program requires a slice to be allocated that is the same size as an input array
+
+// Given a series of numbers, take the running average. In other words, the running average of the last N elements
+
+const std = @import("std");
+
+fn runningAverage(arr: []const f64, avg: [] f64) void {
+ var sum: f64 = 0;
+
+ for (0.., arr) |index, val| {
+ sum += val;
+ avg[index] = sum / @intToFloat(f64, index + 1);
+ }
+}
+
+pub fn main() !void {
+ // pretend this was defined by reading in user input
+ var arr: []const f64 = &[_]f64{ 0.3, 0.2, 0.1, 0.1, 0.4 };
+
+ // initialize the allocator
+ var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
+
+ // free the memory on exit
+ defer arena.deinit();
+
+ // initialize the allocator (TODO: replace this with ???)
+ const allocator = arena.allocator();
+
+ // TODO: replace this whole line with ???
+ var avg = try allocator.alloc(f64, arr.len);
+
+ runningAverage(arr, avg);
+ std.debug.print("Running Average: ", .{});
+ for (avg) |val| {
+ std.debug.print("{d:.2} ", .{val});
+ }
+}
+
+// For more details on memory allocation and the different types of memory allocators, see https://www.youtube.com/watch?v=vHWiDx_l4V0