text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
So how do I get a complete
copy of Zamyla's name? | 2,270.16 | 4.18 |
I need to preemptively do
a little bit of arithmetic | 2,274.34 | 2.63 |
and say, all right, how
long is Zamyla's name? | 2,276.97 | 2.05 |
Well, it's the length
of s, str len of s. | 2,279.02 | 4.06 |
But plus one. | 2,283.08 | 1.223 |
Why plus one? | 2,284.303 | 2.878 |
Why plus one? | 2,287.181 | 1.409 |
Yeah? | 2,288.59 | 2.05 |
Exactly. | 2,290.64 | 0.59 |
We now were hit the lowest
level of the computer. | 2,291.23 | 2.91 |
If we don't ask the operating system for
memory for that extra backslash 0 byte, | 2,294.14 | 5.047 |
we're not going to get it. | 2,299.187 | 1.083 |
So we have to explicitly
say, give me one more byte, | 2,300.27 | 2.266 |
because I know how strings are
implemented underneath the hood. | 2,302.536 | 2.624 |
I need to put that backslash
zero there, ultimately, | 2,305.16 | 2.3 |
and then whatever that expression is,
the length of Zamyla, so Z-A-M-Y-L-A, | 2,307.46 | 4.45 |
six, plus one, seven bytes. | 2,311.91 | 2.95 |
Times the size of a character. | 2,314.86 | 1.9 |
Turns out it's always going
to be one, by definition. | 2,316.76 | 2.26 |
But just for good measure, I'm
clearly saying, give me seven times | 2,319.02 | 3.49 |
the size of a char,
which is going to be one. | 2,322.51 | 2.37 |
That gives me seven total bytes. | 2,324.88 | 1.96 |
So just to simplify. | 2,326.84 | 1.41 |
If you multiply all this out,
because the line looks unnecessarily | 2,328.25 | 2.81 |
cryptic at the moment, this really
is equivalent, at the moment, | 2,331.06 | 6.3 |
to just this. | 2,337.36 | 1.18 |
Call the function malloc. | 2,338.54 | 1.45 |
Give it the number seven, so that
malloc, and in turn, the operating | 2,339.99 | 3.66 |
system, looks inside of its memory
bank, so to speak, and says, | 2,343.65 | 2.7 |
hmm, where are there are seven available
bytes that aren't currently in use? | 2,346.35 | 3.58 |
Ah, here is a chunk of them. | 2,349.93 | 1.73 |
And it's a contiguous chunk. | 2,351.66 | 1.37 |
It's going to find a block of memory,
a rectangular region, if you will, | 2,353.03 | 3.44 |
and grab seven bytes, and
return them to my main function. | 2,356.47 | 6.64 |
But what do I mean to
return a chunk of memory? | 2,363.11 | 2.34 |
Well, just as get
string returns a string | 2,365.45 | 3.92 |
by returning the address of the
first character in that string, | 2,369.37 | 3.95 |
so does malloc equivalently
simply return the address | 2,373.32 | 3.9 |
of the first byte of memory. | 2,377.22 | 3.7 |
But the danger now is
that unlike a string, | 2,380.92 | 3.07 |
malloc is not giving you characters. | 2,383.99 | 1.88 |
It's just giving you seven bytes in
a row that you are now free to use. | 2,385.87 | 3.93 |
It does not give you a backslash
zero at the end of them. | 2,389.8 | 3.19 |
If you want to remember the
length of the chunk of memory | 2,392.99 | 3.43 |
you just allocated, the burden is
entirely on you, the programmer. | 2,396.42 | 3.682 |
And indeed, one of the most common
sources of bugs in writing code in C | 2,400.102 | 2.958 |
is to forget about how long
was this chunk of memory, | 2,403.06 | 2.55 |
and to accidentally, with a loop,
go too far past the end of it. | 2,405.61 | 3.376 |
And we'll see what can
happen in those cases. | 2,408.986 | 1.874 |
So now, assuming I do have in t the
address of that chunk of memory, | 2,410.86 | 3.92 |
let me just say, if t equals
equals null, return 1. | 2,414.78 | 3.73 |
Something happened that's bad,
probably the operating system just | 2,418.51 | 3.21 |
didn't have seven extra
bytes of memory to give me. | 2,421.72 | 2.27 |
So fine, I'll quit. | 2,423.99 | 1.51 |
Then down here, what do I want to do? | 2,425.5 | 1.88 |
Well, I now need to implement, at
least in this example, my own copying | 2,427.38 | 4.98 |
process. | 2,432.36 | 1.46 |
Here, at this point in the story,
I have two variables, s and t. | 2,433.82 | 4.12 |
s contains the address
of Zamyla's name. t | 2,437.94 | 2.975 |
contains the address of a new
chunk of memory of length seven. | 2,440.915 | 3.495 |
So here's what I want to do. | 2,444.41 | 1.22 |
Just like a couple of
weeks ago, I'm going | 2,445.63 | 1.92 |
to iterate from zero on up
to the length of the string. | 2,447.55 | 3.65 |
But not up to, but up through
the length of the string. | 2,451.2 | 2.684 |
Because in this case, I actually
want to iterate with a for loop | 2,453.884 | 2.666 |
up through that backslash 0 byte. | 2,456.55 | 3.33 |
And then just this syntax
from a couple of weeks ago, | 2,459.88 | 2.41 |
when we simply manipulated strings as
for our cryptography ciphers, character | 2,462.29 | 6.06 |
by character. | 2,468.35 | 0.98 |
Make the i-th character of t
equal to the i-th character of s. | 2,469.33 | 3.95 |
And this is perfectly valid,
because so long as this loop doesn't | 2,473.28 | 3.32 |
go past n, the number of characters
that I allocated, seven, in this case, | 2,476.6 | 5.58 |
I can go to t bracket 0, bracket 1,
bracket 2, all the way up through n, | 2,482.18 | 5.64 |
effectively copying the string. | 2,487.82 | 2.66 |
And so now when I actually print out s
and t, I should see truly a copy of t. | 2,490.48 | 8.12 |
Because even when I
force its first character | 2,498.6 | 2.37 |
to lower case with this same
line of code here as before, | 2,500.97 | 3.24 |
I'm actually changing different memory. | 2,504.21 | 2.884 |
So let's compile this. | 2,507.094 | 0.916 |
Make copy 1, ./copy1. | 2,508.01 | 3.62 |
And let me go ahead and type
in zamyla in all lowercase, | 2,511.63 | 3.89 |
and now notice the
program does seem to work. | 2,515.52 | 2.86 |
Zamyla is reprinted in lower
case for s, but it's then | 2,518.38 | 3.67 |
print in uppercase for
its first letter for t. | 2,522.05 | 2.62 |
And because the z's look pretty
similar, let's do my name again, | 2,524.67 | 2.93 |
whereby I type david in all lowercase. | 2,527.6 | 2.47 |
Type Enter. | 2,530.07 | 0.64 |
And now, you see s is
still david all lowercase, | 2,530.71 | 3.14 |
but t has only now been
capitalized itself. | 2,533.85 | 3.36 |
It hasn't had a side
effect of some sort on s, | 2,537.21 | 1.98 |
because they're different
chunks of memory. | 2,539.19 | 2.571 |
Why? | 2,541.761 | 0.499 |
Well, what has just happened
in this program is this. | 2,542.26 | 3.72 |
We have, again, done
string s gets get string. | 2,545.98 | 4.73 |
And when we typed get
string, this gives me | 2,553.34 | 5.52 |
a chunk of memory for the address of s. | 2,558.86 | 2.32 |
Get string gives me a name,
like D-A-V-I-D in all lowercase, | 2,561.18 | 4.84 |
plus that backslash 0. | 2,566.02 | 1.64 |
Which again, is really just an
array underneath the hood like this, | 2,567.66 | 3.6 |
that starts at some byte, and maybe
it's again, by coincidence, 123. | 2,571.26 | 3.446 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.