text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
have two copies of the number one-- in a and in temp.
720.35
3.34
Then, I change the value of a to equal b.
723.69
3.09
So at this point in the story, a should have a value of 2.
726.78
3.92
b should have a value of 2.
730.7
1.62
Which could be problematic, except I've saved the original value 1
732.32
3.88
in my temporary variable.
736.2
1.59
And so, indeed I change b to equal that temporary value.
737.79
3.97
So indeed, I can't do this magical switcheroo.
741.76
2.63
Because if I simultaneously took one value and one value
744.39
3
and put them in each other's places, I would essentially
747.39
2.8
risk clobbering one or the other.
750.19
1.554
Because the computer typically can only do one thing
751.744
2.166
at a time underneath the hood, even though it's super fast.
753.91
2.97
So I run the risk of somehow getting two copies of that same value,
756.88
5.43
unless I'm careful.
762.31
0.96
I really want to take one value, put it in a temporary space.
763.27
3.71
Then, copy one value over.
766.98
1.72
And then, do that switcheroo.
768.7
1.84
But it doesn't seem to work.
770.54
1.54
And yet, this is a good opportunity for one more tool.
772.08
2.98
Let me do a further check.
775.06
1.48
Let me go ahead and run debug 50 on no swap.
776.54
4.5
And you know what?
781.04
0.75
I'm going to set a break point right at the beginning of this program on line
781.79
3.33
10 by simply clicking in the so-called gutter of the line number here.
785.12
3.99
A red dot appears, so that now when I run debug 50,
789.11
3.04
notice my right hand bar pops out.
792.15
2.17
Notice that the line 10 in question is highlighted, because execution is
794.32
3.96
paused at this line.
798.28
1.2
Let's take a look at what's going on the right hand side of my debugger.
799.48
3
Notice that x has a value of 1 and y has a value of 2.
802.48
4.1
And that's what I intend.
806.58
1.15
Now, let's begin to step over these lines.
807.73
2.34
So let's step over the printf.
810.07
1.77
And we see on my terminal window that x is 1.
811.84
2.41
Step over the next printf and we see that y is 2.
814.25
2.99
Now, let's step over that and we see indeed swapping dot dot dot.
817.24
3.62
But rather than step over my call to the swap function, let's step into it.
820.86
4.31
So I can actually see maybe my logic is buggy.
825.17
2.67
So let's go ahead and step into swap.
827.84
2.37
And notice that debug 50 immediately jumps down to the first line and swap.
830.21
4.42
And notice now on the right hand side, what's going on?
834.63
3.17
a is 1 and b is 2.
837.8
2.21
And that's as expected, because I passed an x and y.
840.01
3.22
Swap calls it's two inputs a and b respectively, so a is 1, b is 2.
843.23
4.8
Temp though, very curiously is 32,767.
848.03
6.629
And where is that value coming?
854.659
1.291
Well, it turns out when you declare a variable,
855.95
3.38
but haven't yet said it's of some value it's going to by definition have
859.33
3.58
an undefined value.
862.91
1.18
A so-called garbage value.
864.09
1.51
Remnants of other aspects of your program
865.6
2.77
having used that chunk of memory before.
868.37
2.17
And we'll see why this is in just a bit.
870.54
1.77
So this is anomalous.
872.31
1.25
It's not a bug in the program.
873.56
1.25
It's to be expected, because I haven't yet assigned
874.81
2.22
temp a value until-- let me zoom out.
877.03
3.11
Until I step over line 21 and actually let temp equal a.
880.14
6.58
And notice, if you watch the right hand side of my screen now,
886.72
2.75
temp is currently 32,767.
889.47
4.03
And when I step over it, OK.
893.5
2.51
It becomes 1.
896.01
1.04
So that's as expected.
897.05
1.29
Now, a is about to become b.
898.34
2.61
And indeed, we now see that a is 2 b is 2 and temp is still 1.
900.95
4.2
So now we have to put temp into b, so let's step over that line.
905.15
3.4
And indeed, we've done a switcheroo of a and b.
908.55
3.57
a is 2, b is 1.
912.12
2.18
OK.
914.3
0.5
Let's go ahead and play this out.
914.8
2.55
Let's continue here.
917.35
1.06
I claim that I've swapped.
918.41
1.52
I'm printing x-- and somehow x and y are still 1 and 2.
919.93
6.68
Even though the debunker told me it would
926.61
1.97
seem that my swap function was correct.
928.58
2.33
So what is happening after all of these weeks of CS50 and all of these problems
930.91
4.24
solved thus far?
935.15
0.792
It would seem that all of our understanding of things
935.942
2.208
is kind of unraveling.
938.15
1.059
And that's because we've been very careful over the past few weeks,
939.209
2.791
at least in class.
942
0.93
And in sections to try to avoid tripping over some of these lower-level
942.93
3.41
implementation details.
946.34
1.26
And there's not that many of them.
947.6
1.56
But today, is now a time to peel back this layer
949.16
4.12
and understand exactly what it is that's going on.
953.28
2.83
Indeed, all of this time when you run a program on your computer,
956.11
4.06
double-click an icon or run the program's name with dot slash
960.17
2.89
something or other at your terminal window-- what happens
963.06
2.56
is that your computer gives that program the illusion of a really big chunk
965.62
4.93
of memory all of its own.
970.55
1.34
Maybe two gigabytes of memory, even though it doesn't necessarily
971.89
3.05
use all of that memory.
974.94
1.337
And that memory-- if you just think of your computer's memory,
976.277
2.583
as we've done before, is like a rectangular region.
978.86
2.125
And we could number of the bytes in my memory.
980.985
1.922
But it doesn't really matter what the addresses are, what the numbers are,
982.907
3.083
just that it exists.
985.99
1.78
It turns out that when you run a program,
987.77
3.19