From cbe9dbe0932ed300615b5ca02816906b6ade0419 Mon Sep 17 00:00:00 2001 From: Daniel Welsh Date: Thu, 6 Mar 2025 11:55:43 -0800 Subject: [PATCH] Chapter 3 grammar and typos --- Chapters/01-memory.qmd | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Chapters/01-memory.qmd b/Chapters/01-memory.qmd index 54d71fe0..1faded35 100644 --- a/Chapters/01-memory.qmd +++ b/Chapters/01-memory.qmd @@ -160,7 +160,7 @@ each other. This is a common mistake that beginners have, when seeing "x vs y" s tabloid headlines. These two types of memory are actually complementary to each other. So, in almost every Zig program that you ever write, you will likely use a combination of both. I will describe each memory space in detail over the next sections. But for now, I just want to -stablish the main difference between these two types of memory. +establish the main difference between these two types of memory. In essence, the stack memory is normally used to store values whose length is fixed and known at compile time. In contrast, the heap memory is a *dynamic* type of memory space, meaning that, it's @@ -230,7 +230,7 @@ you don't have the work (or the responsibility) of freeing/destroying these obje Because they will be automatically destroyed once the stack space is freed at the end of the function scope. So, once the function call returns (or ends, if you prefer to call it this way) -the space that was reserved in the stack is destroyed, and all of the objects that were in that space goes away with it. +the space that was reserved in the stack is destroyed, and all of the objects that were in that space go away with it. This mechanism exists because this space, and the objects within it, are not necessary anymore, since the function "finished its business". Using the `add()` function that we exposed above as an example, it means that the object `result` is automatically @@ -278,9 +278,9 @@ would you even consider returning a pointer to one of these objects? This pointe invalid, or, more likely, "undefined". In conclusion, it's totally fine to write a function that returns the local object -itself as result, because then, you return the value of that object as the result. +itself as a result, because then, you return the value of that object as the result. But, if this local object is stored in the stack, you should never write a function -that returns a pointer to this local object. Because the memory address pointed by the pointer +that returns a pointer to this local object. Because the memory address pointed to by the pointer no longer exists. @@ -358,7 +358,7 @@ how much memory is allocated, and where this memory is freed. > Unlike stack memory, heap memory is allocated explicitly by programmers and it won’t be deallocated until it is explicitly freed [@jenny2022]. -To store an object in the heap, you, the programmer, needs to explicitly tells Zig to do so, +To store an object in the heap, you, the programmer, need to explicitly tell Zig to do so, by using an allocator to allocate some space in the heap. In @sec-allocators, I will present how you can use allocators to allocate memory in Zig. @@ -760,7 +760,7 @@ should use `alloc()` and `free()`. But if you need to store just a single item, then, the `create()` and `destroy()` methods are ideal for you. In the example below, I'm defining a struct to represent an user of some sort. -It could be a user for a game, or software to manage resources, it doesn't mater. +It could be a user for a game, or software to manage resources, it doesn't matter. Notice that I use the `create()` method this time, to store a single `User` object in the program. Also notice that I use the `destroy()` method to free the memory used by this object at the end of the scope.