Arrays & Dynamic Memory Address | Godot GDScript Tutorial | Ep 10.1
Summary
TLDRThis episode of the GT Script Fundamentals tutorial explores arrays in memory, explaining how they are stored in contiguous locations. It clarifies that in Godot, arrays are objects with reference counts. The tutorial demonstrates how assigning arrays to new variables shares the same memory reference, leading to changes in one affecting the other. It also delves into 2D arrays, showing how they reference memory addresses for sub-arrays. The script concludes with an explanation of shallow and deep copies, highlighting the importance of copying mechanisms to maintain variable isolation and independence in memory.
Takeaways
- 📚 Arrays in Godot are objects stored in memory, with their own reference count and memory address.
- 🔍 When an array is assigned to another variable, both variables reference the same memory location, meaning changes to one affect the other.
- 🔑 Understanding memory address is crucial when working with arrays in Godot, as it impacts how data is manipulated and stored.
- 💡 The script introduces shorthand notations to simplify the explanation of memory behavior without focusing on individual value addresses.
- 👉 Assignment of arrays to new variables does not create a new copy by default; it shares the same memory reference, leading to shared state.
- 📈 The concept of reference count is fundamental; it increases when a new variable references the same array object.
- 🎯 The script differentiates between shallow and deep copying of arrays. Shallow copies only duplicate the top-level array, while deep copies duplicate all elements, including nested arrays.
- 🌐 2D arrays are arrays of arrays, or sub-arrays, which also follow the same memory referencing rules as simple arrays.
- 🔄 When performing a shallow copy, sub-arrays still reference the original memory location, leading to shared sub-array data between the original and copied arrays.
- 📝 Deep copying is essential for creating completely independent arrays where changes to one do not affect the other, including nested arrays.
- 🚀 The tutorial emphasizes the importance of understanding array behavior in memory for effective data manipulation and isolation in Godot scripting.
Q & A
What are arrays in memory according to the script?
-Arrays in memory are a collection of items stored at contiguous memory locations. They are objects stored in memory and can be manipulated and used in various ways.
What is the basic concept of an array in Godot engine?
-In Godot, arrays are objects stored in memory. A basic single dimension array is a variable assigned with literal values.
What does the script mean by 'reference count' in the context of arrays?
-The 'reference count' refers to the number of variables that are pointing to the same array object in memory. When a variable is assigned to another, the reference count increases.
How does the script describe the memory representation of an empty array variable?
-An empty array variable points to a specific area in memory with a type (in this case, array) and a reference count, which is initially one because only one item is pointing to it.
What happens to the reference count when a new variable is assigned to an existing array?
-When a new variable is assigned to an existing array, the reference count of the array object in memory increases by one because there is now another variable pointing to it.
Why does the script focus on the memory address location of the array rather than the values?
-The script focuses on the memory address location of the array to emphasize how arrays behave in memory and how they are manipulated, without getting into the details of the individual values' memory addresses.
What is the difference between assigning an array to another variable and using the 'duplicate' function?
-Assigning an array to another variable creates a reference where both variables point to the same memory location, while using the 'duplicate' function creates a new copy of the array with its own separate memory location.
How does the script explain the behavior of 2D arrays in memory?
-The script explains that 2D arrays are arrays inside an array, or sub-arrays. When a 2D array is assigned to a new variable, both the main array and the sub-arrays reference the same memory address unless a 'deep copy' is performed.
What is the purpose of the 'shallow copy' in the context of arrays?
-A 'shallow copy' of an array creates a new array with the same top-level elements but with references to the same sub-arrays, meaning changes to the sub-arrays in the original array will affect the copied array.
What does a 'deep copy' achieve in terms of array memory management?
-A 'deep copy' creates a completely independent copy of an array, including all sub-arrays, with new memory addresses. Changes to the sub-arrays in the copied array do not affect the original array.
Why is the 'deep copy' method important when you want to avoid affecting the original array?
-The 'deep copy' method is important because it ensures that the copied array and its sub-arrays have their own separate memory locations, thus changes made to the copied array do not impact the original array.
Outlines
📚 Understanding Arrays in Memory
This paragraph introduces the concept of arrays in Godot's memory management system. Arrays are objects stored in contiguous memory locations, and the script explores how they are represented in memory. It explains the reference count mechanism, which increases when multiple variables point to the same array object. The paragraph also clarifies that arrays in Godot are simple objects and not just primitive data types. It emphasizes the importance of understanding memory address behavior when working with arrays, especially when assigning arrays to new variables, which results in both variables referencing the same memory location.
🔍 Deep Dive into 2D Arrays and Memory References
The second paragraph delves into the intricacies of 2D arrays, which are essentially arrays containing sub-arrays. It illustrates how memory addresses work with 2D arrays, showing that sub-arrays are references to other memory locations. The paragraph explains the implications of assigning a 2D array to a new variable, which results in both variables sharing the same memory references, thus affecting each other when values are changed. It also introduces the 'duplicate' function, which can perform either a shallow or deep copy of an array, with the former only copying the top-level array and the latter creating entirely new memory references for all elements, including sub-arrays.
💡 The Significance of Shallow and Deep Copies
The final paragraph focuses on the practical applications of shallow and deep copying in Godot's script. It provides examples of how shallow copying duplicates the top-level array but maintains the original memory references for sub-arrays, leading to shared changes between the original and copied arrays. In contrast, deep copying creates entirely new memory addresses for all elements, including nested sub-arrays, ensuring that changes to the copied array do not affect the original. The paragraph concludes with a demonstration of how to use the 'duplicate' method with a boolean parameter to specify the type of copy, emphasizing the importance of choosing the correct method to maintain data integrity and isolation between variables.
Mindmap
Keywords
💡Arrays
💡Memory Address
💡Reference Count
💡Shallow Copy
💡Deep Copy
💡Sub-arrays
💡Variable Assignment
💡Indexing
💡Memory Behavior
💡Isolation
Highlights
Introduction to the episode focusing on arrays and memory address in Godot.
Explanation of arrays as collections of items stored at contiguous memory locations.
Description of arrays as objects stored in memory in Godot.
Introduction to single-dimension arrays and their behavior in memory.
Clarification that variables point to areas in memory and have reference counts.
Demonstration of how assigning one array variable to another affects memory reference counts.
Illustration of how changing values in arrays affects reference counts and memory locations.
Overview of how arrays and their values are represented in memory addresses.
Explanation of assigning an array to a new variable and its effect on memory addresses.
Detailed look at how memory behaves with a 2D array in Godot.
Discussion of sub-arrays and their reference locations in memory.
Introduction to shallow copies and deep copies of arrays in Godot.
Explanation of the duplicate function and its options for shallow and deep copies.
Example of a shallow copy and its impact on memory references and sub-arrays.
Example of a deep copy and its impact on memory references and sub-arrays.
Conclusion emphasizing the importance of understanding memory addresses in array manipulation.
Transcripts
hello and welcome to another episode in
the GT script fundamental tutorial
series in this episode we'll be taking a
look at arrays in memory address arrays
are a collection of items stored at
contiguous memory locations we know how
to manipulate and use arrays but let's
go ahead and take a look at how arrays
behave in memory so in Godot arrays are
objects that are stored in memory the
key thing to keep in mind is that arrays
are just objects now this is a basic
single dimension array as you can see
here we've declared a variable and we've
assigned it an array I like to call
these simple arrays because they're just
literal values before we begin I need to
go over some shortcuts I'm going to be
doing in this slideshow this is because
I just want to focus on the memory
address location over the whole picture
but to preface when you create an empty
variable what you're really doing is
you're taking that variable and you're
pointing it to an area and memory
address and this is what it would look
like you would have a type in this case
array and you would have a reference
count and it's one because one item is
pointing to it now let's say you do
variable another array is equal to empty
array well you're really pointing to
this object so it's reference count goes
up to two that's basically the basics of
how it looks like in memory now another
shortcut I'm gonna do in this video is
the following I'm just going to put an
integer inside a block to represent how
it behaves in memory but in reality what
we're really doing is when we do have an
index we're actually just pointing to
another object in memory that of course
keeps count of its reference or
reference count so when we go ahead and
add so when we go ahead and add a value
in this case 1 what we're really doing
is we're pointing to that same object in
memory and we're increasing the
reference count by 1 and of course when
we go ahead and change that value we're
just pointing to a different object in
memory and changing its value adding a
reference count of 1 and decreasing the
reference count of the other now this
episode we only really care about the
reference count or rather the location
and address memory of the array rather
than the address memory of the values so
that's all I want to do is
just preface but we're not gonna pay any
attention to this all we care about is
when I do short hands it's really
representing this but even when I do
shorthand our main focus for this
episode is just the memory address
location of the array and not the memory
address locations of the values this is
what a simple array or rather our my
array variable would look like in memory
as you can see here we have our variable
my array and it's actually pointing to
somewhere in our memory address keep in
mind this is a basic simplification of
how arrays behave in memory but
nonetheless this is how it would
normally behave variable points to a
location memory address we have our
indexes 0 1 2 and we have our values 1 2
3 keep in mind that location memory
address starts at the beginning of your
array now let's go ahead and take a look
at what happens when we try to assign
that variable my array into a different
variable in this case our new variable
copy array will be assigned the values
from my array when we print this out
into console using the print statement
it will return 1 2 & 3
same thing for copy array 1 2 & 3 but
when you assign an array to another
variable how does that look like in
memory well as you can see here we have
a variable copy array and it actually
references the same array in memory
location as the my array the specific
reasons for this can get really deep and
it is considered its own topic however
to keep things simple just remember when
you do direct manipulations such as copy
array equals my array keep in mind we're
not using a duplicate function what
you're really doing is you're saying
wherever my variable my array is
referencing in this case location 100
assigned that reference location to the
variable copier what this means is that
if we were to change a value let's say
the value at index 0 which is 1 if we
were to change 1 into 100 what will
happen is that copy array and my array
will have their indexes changed to a
hundred because they're both pointing
to the same memory address location so
to take a look at that we have our
assignment like in our previous slide my
array at index 0 is equal to a hundred
again notice how we're using my array
and we're assigning it a hundred and
notice how when we're printing copy
array a different variable name it will
also change its value to a hundred even
though my array and copy array both
started with the values one two and
three again this is because copy array
and my array both point to the same
reference in address memory and so
changing the value of one will actually
change the value of the other now let's
go ahead and take a look at how memory
behaves with a 2d array and all 2d array
is is an array inside an array which can
also be referred to as a sub array so
our variable 2d array has the value 0
and then a sub array with the values 1
and 2 when we create our variable 2d
array and assign it a value what we do
is we reference a location in memory
address and as you can see here our
index at 0 has the value 0 but notice
how our value at index 1 is this
reference in memory address that's
because this is how arrays behave in
memory for GT script when we create a
sub array what we're really doing is
we're calling a reference to another
location and address memory and then
from there we continue our values in
this case index 0 is 1 and index 1 is 2
so you can see how this works if we want
to call our sub array we call 2d array
index 1 and then index 0 for the value 1
or index 1 for the value of 2 now if we
take this 2d array and we assign it to a
new variable in this case we're calling
our new variable copy 2d this is what it
looks like what we're doing is we're
taking the reference to location address
memory we're assigning that location to
our copy today's so they both point to
not only the first section of the array
but also the sub array so if I were to
change a value here instead of 1 we turn
this into a hundred what happens is when
we do a print statement to either copy
to
and even 2d array it's gonna show zero
new value and then - because we're
referencing the same location address
memory for both variables now this is
where the duplicate function comes into
play many times when you create a
variable you do not want to share the
values between two different variables
the whole point of variables in most
cases is basically isolation isolation
meaning when you change one variable you
should not be affecting other variables
so the duplicate function comes with two
choices you can do a shadow copy or you
can do a deep copy now if you do a
shallow copy and keep in mind that
shallow copies only makes a copy of the
surface of your array this means that
all sub arrays will still point to a
reference in memory basically the same
reference that your copied array is
pointing to now a deep copy will make a
copy of everything including sub arrays
so let's go ahead and take a look at
what that would look like in address
memory here's an example of shallow copy
we have a variable with a basic array
and then we use the arrays built-in
method called duplicate and we pass in
the value of false by passing the
boolean value false we are telling this
method to make a shallow copy of our
basic array and then pass that in to
copy array when you use a shallow copy
what you're saying to the system the
compiler is that you want to take just a
shallow copy of the array and put it
into a different location in memory
address my array has an array at
location memory address 100 by doing a
shallow copy you say variable copy array
take the values from this location and
then put that in a different memory
address location we can point to instead
so as you can see here we have our
location at 200 now if we change the
values of my array we will not affect
copy array and if we change a value and
copy array we will not affect my array
let's go ahead and take a look at what a
shallow copy looks like when you're
dealing with sub arrays so as you notice
here we have a sub array one two sub
array with three four and we're also
passing the false value letting the
compiler know that we want to do a
shallow copy of my array and pass that
into copy array so we have my array one
two three four and it creates it sub
in location memory address 100 the
values in index 0 and 1 are 1 & 2 and
just as we would expect our value in
index 2 is just a reference to another
address memory location where we
continue the sub array index 0 3 & index
1 4 so when we duplicate a rather when
we when we do a shallow copy notice
we're using the false value and our
duplicate function when we do a shallow
copy and we pass it into a new variable
this is what it looks like so when we do
a shallow copy notice how we copy
everything in the first layer we we take
everything and we basically duplicate it
we create another location in 300 and
notice how we have 1 & 2 separated from
location 100 when we change indexes 0 n
1 in my array we do not affect copy
arrays values and vice versa however we
also copied my arrays reference to
memory address location for its sub
array so as you can see here our sub
arrays are both pointing to location 200
and if you were to change a value in
this sub array it will affect how my
array works and it affects how copy
array works so just keep that in mind
you are copying everything including the
reference to location and address memory
now let's go ahead and do a deep copy so
we have the same values in my array but
this time instead of passing the fossil
in we're passing the true value in to
our duplicate method and by doing this
we're telling the compiler to do a
basically deep copy take all the values
take all the sub arrays and give them
their own separate memory address
location so again when we create my
array we're creating it in memory
address location and we're creating a
sub array in a different memory address
location let's go ahead and take a look
at a deep copy and how it looks like in
memory address so when we use the
duplicate method and pass in a true
boolean what we're telling the compiler
is we want to do a deep copy to my array
and then we want to pass that value in
to copy array so what that does is we
look at the memory address for my array
and we say ok let's copy this and put it
into memory address 300 but when you do
the copy you also look at its references
and then you go to the references and
you say hey let's go ahead and take the
sub-array and we'll add it to a new
address in memory location where we will
then pass a new reference to our copy
array basically the array we want to do
a deep copy for we pass the reference
value or the new reference to memory
address location to the new array so
that's how we get the value 400 there's
a lot going on but just know that at the
end of the day this is what it looks
like in memory address so now when we
change the sub-array of copy array we
will not affect my arrays sub array
because the sub array in my array is
pointing or rather referencing an
address location that is not the address
location our copy array is pointing to
so in this case we're pointing to 200
copy raised pointing to 400 when we do
this copy we get the same exact values
if we were to compare them for equality
they would be equal but if we were to
compare them to their memory address
location they'll come back false because
everything in the deep copy is pointing
to a different address in memory
location versus our original array well
I hope you learned a lot today thank you
to everyone who has subscribed to my
channel and thank you for clicking the
like button there is no github project
for this episode so I hope to see you in
the next episode have a wonderful day
Ver Más Videos Relacionados
Arrays | Godot GDScript Tutorial | Ep 10
Strings (Basics of Memory) | Godot GDScript Tutorial | Ep 10.2
Memory, Cache Locality, and why Arrays are Fast (Data Structures and Optimization)
Life Cycle & Reference Counting | Godot GDScript Tutorial | Ep 1.2
PHP Variable Storage & Object Comparison - Zend Value (zval) - Full PHP 8 Tutorial
Go (Golang) Tutorial #5 - Arrays & Slices
5.0 / 5 (0 votes)