text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
Just because it's easy to say, but that's not
2,574.706
1.874
where it's necessarily going to end up.
2,576.58
1.624
And so what ends up here is 123.
2,578.204
3.176
And then later, when I allocate t, I again
2,581.38
3.95
get this little chunk of memory that's supposed
2,585.33
3.08
to store the address of a character.
2,588.41
2.41
And actually, recall that we're now doing this
2,590.82
2.22
as char star, not even string.
2,593.04
3.31
So t is similarly a char star.
2,596.35
2.36
And what happens, malloc, when I ask it for seven bytes,
2,598.71
3.57
gives me 1, 2, 3, 4, 5, 6, 7 bytes of memory.
2,602.28
5.47
There's no null terminating character just yet.
2,607.75
2.02
It's just a block of memory.
2,609.77
1.19
And frankly, there could be some random values here, as
2,610.96
3.42
denoted with question marks here.
2,614.38
1.79
It's just a chunk of memory that might have been used previously
2,616.17
2.68
in my program for some other purpose.
2,618.85
1.66
But what gets stored here, if this happens to be at address 234,
2,620.51
5.12
is this value here, 234.
2,625.63
2.85
And if you're not liking the numbers, again, you
2,628.48
2.18
can think of these as just being pointers, arrows,
2,630.66
3.85
to these chunks of memory.
2,634.51
1.74
But now, in my C code, when I have a few lines above this loop whereby
2,636.25
9.24
I am copying from s bracket i into t bracket
2,645.49
3.79
i, each of the characters in my loop, what's actually happening?
2,649.28
5.2
Well, fairly intuitively, this lower case d ends up getting copied here.
2,654.48
4.96
This lower case a ends up getting copied here.
2,659.44
2.692
V-I-D, on through.
2,662.132
3.218
And David can't count, so-- backslash-- oh, right.
2,665.35
3.467
David's name is shorter than Zamyla's name, which means we didn't actually
2,668.817
3.083
ask for this many characters over here.
2,671.9
2.79
But we have taken the computer more literally now.
2,674.69
2.49
Give me six bytes, not seven bytes, in this case.
2,677.18
2.53
And then literally copy each of the characters from the original string
2,679.71
3.94
into this new string all the way up through that backslash 0 character.
2,683.65
5
And then when you capitalize the first character in t,
2,688.65
3.73
you are literally only changing this-- we can do better than this.
2,692.38
5.27
We are only changing this first character here,
2,697.65
4.22
which looks like that now.
2,701.87
3.46
And that's what's going on underneath the hood.
2,705.33
2.54
So this is why, then, in the beginning of the class,
2,707.87
2.18
we don't introduce strings as char stars,
2,710.05
2.19
because you very quickly get caught up in a lot of this minutia.
2,712.24
2.797
But at the end of the day, it's not all that complicated
2,715.037
2.333
once you realize that a string is just an address,
2,717.37
2.95
and malloc, this new function, also just returns an address.
2,720.32
3.25
This is very powerful, because now you have
2,723.57
2.17
these sort of breadcrumbs that can lead you to different places in memory.
2,725.74
4.15
A little map, so to speak, that can lead you to actual strings in memory,
2,729.89
3.95
and can actually now solve problems more effectively.
2,733.84
2.78
For instance, we can go back and solve one other problem
2,736.62
2.47
we saw a moment ago, which was swap.
2,739.09
4.17
So this version of swap was broken why?
2,743.26
4.18
What was the source of this fundamental problem?
2,747.44
3.99
Yeah?
2,751.43
1.319
STUDENT: [INAUDIBLE]
2,752.749
0.833
DAVID J. MALAN: Yeah.
2,755.765
0.875
When you went back to main, you erased the memory on top of it,
2,756.64
3
and the fundamental problem there was when I passed in x and y,
2,759.64
5.55
they became copies called a and b, in different chunks of memory.
2,765.19
4.48
And so the fundamental problem seems to be
2,769.67
2.93
that swap is incorrectly implemented.
2,772.6
2.61
It's logically correct.
2,775.21
1.58
It does swap two values.
2,776.79
1.2
And we saw that with debug 50, but it's kind of fundamentally flawed in so far
2,777.99
4.77
as it requires, it seems, by design of C,
2,782.76
3.81
that a and b be passed in by value as copies, so to speak.
2,786.57
4.8
We need some way to change this function to say main, hey, uh-uh, don't give me
2,791.37
4.62
copies of your variables.
2,795.99
1.58
Give me a treasure map that will lead me to your variables.
2,797.57
3.49
Give me the address of x.
2,801.06
2.12
Give me the address of y.
2,803.18
1.91
And I'll still call them a and b, or whatever I want,
2,805.09
2.37
but lead me to the original values.
2,807.46
2.5
Don't just pass me copies of those values.
2,809.96
2.93
And so we can change swap as follows from a program or a function that's
2,812.89
4.32
incorrect entirely, but into one that is correct.
2,817.21
5.23
And we need to change the syntax a little bit.
2,822.44
2.12
So before is what we had here.
2,824.56
3.2
After is what we now have.
2,827.76
2.23
Before, after.
2,829.99
2.12
Before, after.
2,832.11
1.619
So if you see it in rapid succession there,
2,833.729
1.791
all you see is that a whole bunch of stars are appearing in the code.
2,835.52
3.64
And unfortunately, C was not designed in the best of ways
2,839.16
3.85
to make clear what star means in different contexts,
2,843.01
2.57
but it's all related as follows.
2,845.58
2.14
The fact that I have now changed a and b to be not ints,
2,847.72
4.61
but ints stars, int pointers, if you will, means that when main calls swap,
2,852.33
7.42
it is by design of the C language, going to pass in the address of x
2,859.75
4.07
and the address of y.
2,863.82
0.95
So that's what the star means.
2,864.77
1.25
Give me the address of an int and the address of an int, not actual ints.
2,866.02
4.08
Now, down here, the star unfortunately means something slightly different,
2,870.1
3.23
but related in spirit.
2,873.33
1.79
Int temp just gives me an integer, an int variable called temp.
2,875.12
4.78
Star a, in this context, without the word int in front of it again,
2,879.9
5.01
means go to that location.
2,884.91
2.66
Follow the treasure map, so to speak.
2,887.57
1.8
Go to the address that is in a.
2,889.37
2.71