Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 3 grammar and typos #148

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Chapters/01-memory.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.


Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.
Expand Down