From 1be1d854a8f690c25a577bef1feb1e9926caf2c7 Mon Sep 17 00:00:00 2001 From: Sean Aubin Date: Tue, 28 Feb 2023 20:54:42 -0500 Subject: first draft of memory allocation exercise --- exercises/076a_memory_allocation.zig | 70 ++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 exercises/076a_memory_allocation.zig (limited to 'exercises') 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 -- cgit v1.2.3 From f01d9ba92d8362557aa0e4885d48de5e7975f67d Mon Sep 17 00:00:00 2001 From: Sean Aubin Date: Wed, 8 Mar 2023 12:50:10 -0500 Subject: rephrase/reformat comments --- exercises/076a_memory_allocation.zig | 43 ++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 19 deletions(-) (limited to 'exercises') diff --git a/exercises/076a_memory_allocation.zig b/exercises/076a_memory_allocation.zig index b098773..ff54edf 100644 --- a/exercises/076a_memory_allocation.zig +++ b/exercises/076a_memory_allocation.zig @@ -1,22 +1,22 @@ -// 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: +// 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. +// 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. +// You'll need to request memory for your 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: +// 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 +// // memory allocation can fail, so the return type is !void // pub fn main() !void { -// +// // var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); // defer arena.deinit(); // @@ -25,17 +25,21 @@ // const ptr = try allocator.create(i32); // std.debug.print("ptr={*}\n", .{ptr}); // -// const slice_ptr = try allocator.create(i32); +// const slice_ptr = try allocator.create(f64, 5); // 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 +// Instead of an simple integer or a constant sized slice, 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 +// Given a series of numbers, take the running average. In other +// words, each item N should contain the average of the last N +// elements. const std = @import("std"); -fn runningAverage(arr: []const f64, avg: [] f64) void { +fn runningAverage(arr: []const f64, avg: []f64) void { var sum: f64 = 0; for (0.., arr) |index, val| { @@ -54,11 +58,11 @@ pub fn main() !void { // free the memory on exit defer arena.deinit(); - // initialize the allocator (TODO: replace this with ???) + // initialize the allocator const allocator = arena.allocator(); - // TODO: replace this whole line with ??? - var avg = try allocator.alloc(f64, arr.len); + // allocate memory for this array instead of empty initialization + var avg: []f64 = {}; runningAverage(arr, avg); std.debug.print("Running Average: ", .{}); @@ -67,4 +71,5 @@ pub fn main() !void { } } -// For more details on memory allocation and the different types of memory allocators, see https://www.youtube.com/watch?v=vHWiDx_l4V0 +// For more details on memory allocation and the different types of +// memory allocators, see https://www.youtube.com/watch?v=vHWiDx_l4V0 -- cgit v1.2.3 From f08af9b24280090c599e30a3ee90b4fbfbcb3aaf Mon Sep 17 00:00:00 2001 From: Sean Aubin Date: Wed, 8 Mar 2023 12:51:07 -0500 Subject: move memory allocation example to 095 --- exercises/076a_memory_allocation.zig | 75 ------------------------------------ exercises/095_memory_allocation.zig | 75 ++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 75 deletions(-) delete mode 100644 exercises/076a_memory_allocation.zig create mode 100644 exercises/095_memory_allocation.zig (limited to 'exercises') diff --git a/exercises/076a_memory_allocation.zig b/exercises/076a_memory_allocation.zig deleted file mode 100644 index ff54edf..0000000 --- a/exercises/076a_memory_allocation.zig +++ /dev/null @@ -1,75 +0,0 @@ -// 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. -// 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 your 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, 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(f64, 5); -// std.debug.print("ptr={*}\n", .{ptr}); -// } - -// Instead of an simple integer or a constant sized slice, 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, each item N should contain the 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 - const allocator = arena.allocator(); - - // allocate memory for this array instead of empty initialization - var avg: []f64 = {}; - - 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 diff --git a/exercises/095_memory_allocation.zig b/exercises/095_memory_allocation.zig new file mode 100644 index 0000000..ff54edf --- /dev/null +++ b/exercises/095_memory_allocation.zig @@ -0,0 +1,75 @@ +// 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. +// 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 your 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, 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(f64, 5); +// std.debug.print("ptr={*}\n", .{ptr}); +// } + +// Instead of an simple integer or a constant sized slice, 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, each item N should contain the 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 + const allocator = arena.allocator(); + + // allocate memory for this array instead of empty initialization + var avg: []f64 = {}; + + 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 -- cgit v1.2.3 From 551a36f7c92251e8e035ac9f9f878849af85a459 Mon Sep 17 00:00:00 2001 From: Sean Aubin Date: Wed, 8 Mar 2023 13:09:41 -0500 Subject: fix allocate example --- exercises/095_memory_allocation.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'exercises') diff --git a/exercises/095_memory_allocation.zig b/exercises/095_memory_allocation.zig index ff54edf..057b420 100644 --- a/exercises/095_memory_allocation.zig +++ b/exercises/095_memory_allocation.zig @@ -25,7 +25,7 @@ // const ptr = try allocator.create(i32); // std.debug.print("ptr={*}\n", .{ptr}); // -// const slice_ptr = try allocator.create(f64, 5); +// const slice_ptr = try allocator.alloc(f64, 5); // std.debug.print("ptr={*}\n", .{ptr}); // } -- cgit v1.2.3 From 484822a8da1361d5878c82b0f30eafec1e3bc43e Mon Sep 17 00:00:00 2001 From: Sean Aubin Date: Wed, 8 Mar 2023 13:42:21 -0500 Subject: rename memory allocation 095 -> 096 --- exercises/095_memory_allocation.zig | 75 ------------------------------------- exercises/096_memory_allocation.zig | 75 +++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 75 deletions(-) delete mode 100644 exercises/095_memory_allocation.zig create mode 100644 exercises/096_memory_allocation.zig (limited to 'exercises') diff --git a/exercises/095_memory_allocation.zig b/exercises/095_memory_allocation.zig deleted file mode 100644 index 057b420..0000000 --- a/exercises/095_memory_allocation.zig +++ /dev/null @@ -1,75 +0,0 @@ -// 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. -// 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 your 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, 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.alloc(f64, 5); -// std.debug.print("ptr={*}\n", .{ptr}); -// } - -// Instead of an simple integer or a constant sized slice, 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, each item N should contain the 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 - const allocator = arena.allocator(); - - // allocate memory for this array instead of empty initialization - var avg: []f64 = {}; - - 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 diff --git a/exercises/096_memory_allocation.zig b/exercises/096_memory_allocation.zig new file mode 100644 index 0000000..057b420 --- /dev/null +++ b/exercises/096_memory_allocation.zig @@ -0,0 +1,75 @@ +// 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. +// 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 your 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, 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.alloc(f64, 5); +// std.debug.print("ptr={*}\n", .{ptr}); +// } + +// Instead of an simple integer or a constant sized slice, 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, each item N should contain the 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 + const allocator = arena.allocator(); + + // allocate memory for this array instead of empty initialization + var avg: []f64 = {}; + + 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 -- cgit v1.2.3 From be87134397d1728b3d98d1229d241254130e0708 Mon Sep 17 00:00:00 2001 From: Sean Aubin Date: Wed, 8 Mar 2023 19:22:17 -0500 Subject: change empty init to '???' placeholder --- exercises/096_memory_allocation.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'exercises') diff --git a/exercises/096_memory_allocation.zig b/exercises/096_memory_allocation.zig index 057b420..7843b93 100644 --- a/exercises/096_memory_allocation.zig +++ b/exercises/096_memory_allocation.zig @@ -61,8 +61,8 @@ pub fn main() !void { // initialize the allocator const allocator = arena.allocator(); - // allocate memory for this array instead of empty initialization - var avg: []f64 = {}; + // allocate memory for this array + var avg: []f64 = ???; runningAverage(arr, avg); std.debug.print("Running Average: ", .{}); -- cgit v1.2.3