text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
your operating system typically lays
out your program's memory in a fairly | 990.96 | 3.79 |
standard way. | 994.75 | 1.65 |
There's a chunk of memory down here for
something called environment variables. | 996.4 | 3.52 |
There's a bigger growable chunk
of memory down here called | 999.92 | 3.66 |
the so-called stack. | 1,003.58 | 1.58 |
On the opposite side of the
picture is a so-called heap. | 1,005.16 | 2.379 |
Another chunk of memory that actually
grows in the other direction. | 1,007.539 | 2.791 |
So long story short, bad things
can happen if both of those | 1,010.33 | 3.04 |
grow bigger than you intend. | 1,013.37 | 1.78 |
Then, there's some kind of uninitialized
and initialized data up top. | 1,015.15 | 3.53 |
And then, text. | 1,018.68 | 1.39 |
Now, it turns out text is
the segment of memory where | 1,020.07 | 3.2 |
your program's zeros and ones live. | 1,023.27 | 2.299 |
So when you double-click
an icon on your Mac or PC | 1,025.569 | 2.111 |
or when you run the command
dot slash something, | 1,027.68 | 2.07 |
those zeros and ones are loaded from
your hard disk or solid state disk | 1,029.75 | 3.54 |
into RAM or memory. | 1,033.29 | 1.64 |
And it's put conceptually
at the top of the memory | 1,034.93 | 3.38 |
that your computer program is using. | 1,038.31 | 1.64 |
And below that is the actual
data that your program is using. | 1,039.95 | 4.149 |
The variables and the
values inside of it. | 1,044.099 | 2.451 |
Now, each of these types of
memory have different purposes. | 1,046.55 | 3.06 |
And we'll see in just a moment
what it is that's going on. | 1,049.61 | 3.49 |
And we'll ultimately
peel back these layers. | 1,053.1 | 2.31 |
So what is it that's actually
going on underneath the hood here? | 1,055.41 | 3.53 |
Well, let's consider this
to be my computer's memory. | 1,058.94 | 2.77 |
So focusing on just that
bottom most portion, | 1,061.71 | 2.99 |
which I called the stack a moment ago. | 1,064.7 | 2.08 |
So if we draw just the bottom of my
computer's memory kind of like this, | 1,066.78 | 3.645 |
the bottom of it has technically
environment variables. | 1,070.425 | 2.574 |
But let's focus on the
region known as the stack. | 1,072.999 | 2.041 |
And the stack, as the name implies,
is kind of like the stack of trays | 1,075.04 | 3.68 |
that you might see in a
cafeteria or a dining hall, | 1,078.72 | 2.18 |
where you put trays on top of another
until it can get potentially pretty | 1,080.9 | 3.89 |
tall. | 1,084.79 | 0.62 |
And it turns out when
you run a program, not | 1,085.41 | 2.74 |
only is your program given the illusion
of this really big memory space laid | 1,088.15 | 3.51 |
out as proposed, but
it also by convention | 1,091.66 | 3.23 |
uses this memory in a
fairly standard way. | 1,094.89 | 2.94 |
Specifically, when main is called,
main is given a chunk of memory | 1,097.83 | 5.47 |
at the bottom, so to
speak, of this stack space. | 1,103.3 | 2.48 |
And so, let me go ahead
here and write main. | 1,105.78 | 2.67 |
And any local variables that main
has and any arguments to main, | 1,108.45 | 5 |
namely argc an argv, end up inside here. | 1,113.45 | 3.5 |
So if indeed you are using
something like argv and argc, | 1,116.95 | 4.68 |
you might have a value
like this down here. | 1,121.63 | 2.83 |
And you might have another chunk
of memory carved out here for argv. | 1,124.46 | 4.46 |
And if you have a couple of
local variables, for instance x | 1,128.92 | 4.03 |
and another one, y, those two would
be allocated space in this slice | 1,132.95 | 5.48 |
if you will. | 1,138.43 | 0.53 |
This frame of memory. | 1,138.96 | 2.21 |
Meanwhile, if main calls a
function, like swap-- swap | 1,141.17 | 4.72 |
is allocated a swath of memory, a
frame of memory, above main by design. | 1,145.89 | 5.45 |
So if I've called swap,
its memory ends up here. | 1,151.34 | 3.56 |
And if the swap function
itself has arguments, | 1,154.9 | 3.26 |
like a and b or any
other local variables, | 1,158.16 | 2.94 |
those values too are put inside
of the so-called stack frame. | 1,161.1 | 4.1 |
So this might be a and this might be b. | 1,165.2 | 3.659 |
In other words, the concepts
that we've been taking for | 1,168.859 | 2.291 |
granted, both in Scratch and in C, at
the end of the day, boil down to values | 1,171.15 | 4.48 |
needing to go somewhere physically. | 1,175.63 | 2.1 |
And so, if you assume that
the big rectangular region | 1,177.73 | 2.31 |
here is your computer's memory. | 1,180.04 | 1.44 |
And then, you consider that
the operating system really | 1,181.48 | 2.49 |
just slices and dices this memory,
such that mains memory is down here. | 1,183.97 | 5.16 |
Any function that main calls
is immediately above it. | 1,189.13 | 2.6 |
And frankly, if swap called its own
function, it would end up above it. | 1,191.73 | 4.54 |
But now, given this basic
definition of memory management | 1,196.27 | 4.16 |
and the layout of computer
program's memory space, | 1,200.43 | 2.87 |
you can perhaps start to infer
why all of these failures | 1,203.3 | 4.23 |
have started to happen in my program. | 1,207.53 | 2.08 |
A moment ago, I didn't
have argv and argc. | 1,209.61 | 2.88 |
I just had for instance x and y. | 1,212.49 | 2.04 |
And I had the value 1
and I had the value 2. | 1,214.53 | 4.21 |
Main then called swap and put a copy
of 1 there and a copy of 2 there. | 1,218.74 | 6.53 |
And indeed, that's the key insight. | 1,225.27 | 2.34 |
When a function calls another function,
passing in arguments as inputs, | 1,227.61 | 4.55 |
that the function is being
passed to copies of those inputs. | 1,232.16 | 3.72 |
So at this point in time, if you
opened up the lid of your computer | 1,235.88 | 3.01 |
and looked inside digitally,
you would see 1 and 2 down here. | 1,238.89 | 3.302 |
And you would see
another pattern of bits | 1,242.192 | 1.708 |
representing 1 and 2
up here in duplicate. | 1,243.9 | 2.87 |
So now when my swap
function operates, it | 1,246.77 | 2.98 |
declares a temporary
variable recall, called temp. | 1,249.75 | 2.69 |
So let me draw that here. | 1,252.44 | 2.62 |
And as I recall, it stores
in temp, which value? | 1,255.06 | 3.97 |
The value of a. | 1,259.03 | 1.52 |
The value of a is 1. | 1,260.55 | 2.11 |
It then took the value of b, put it
in a-- which puts that value here. | 1,262.66 | 7.47 |
And now at this point in the
story, a and b are incorrect. | 1,270.13 | 3.15 |
We still need to put the value 1 in b. | 1,273.28 | 2.45 |
And that's why we then took
temps value, put it in b. | 1,275.73 | 3.93 |
Thereby giving the me ultimately
the number 1 in b's slot as well. | 1,279.66 | 7.08 |
And so, at this point in the
story, temp still exists. | 1,286.74 | 3.1 |
And a and b have the correct
answers, 2 and 1 respectively. | 1,289.84 | 3.79 |
But the catch is the moment
that swap returns, this happens. | 1,293.63 | 6.87 |
Essentially, everything that was
being used above main disappears. | 1,300.5 | 4.36 |
It's not actually deleted. | 1,304.86 | 1.099 |
All of those bits are still
there, so technically the numbers | 1,305.959 | 2.541 |
are still there. | 1,308.5 | 0.67 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.