text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
>> So you have things that look
like this inside of your laptop, | 3,538.06 | 2.83 |
or slightly bigger things
inside of your desktop. | 3,540.89 | 2.43 |
But the key is you only have a
finite number of these things. | 3,543.32 | 4.12 |
And there's only a finite amount of
hardware sitting on this desk right | 3,547.44 | 3.79 |
here. | 3,551.23 | 0.5 |
>> So, surely, we can't store
infinitely long numbers. | 3,551.73 | 4.19 |
And, yet, if you think back to
grade school, how many digits can | 3,555.92 | 3.11 |
you have to the right
of a decimal point? | 3,559.03 | 2.37 |
For that matter, how many digits can
you have to the left of a decimal point? | 3,561.4 | 3.28 |
Really, infinitely many. | 3,564.68 | 1.62 |
>> Now, we humans might only
know how to pronounce million, | 3,566.3 | 4.54 |
and billion, trillion, and
quadrillion, and quintillion. | 3,570.84 | 4.15 |
And I'm pushing the limits of my
understanding-- or my-- I understand | 3,574.99 | 4.38 |
numbers, but my
pronunciation of numbers. | 3,579.37 | 1.74 |
But they can get infinitely large with
infinitely many digits to the left | 3,581.11 | 3.61 |
or to the right of a decimal point. | 3,584.72 | 2.33 |
>> But computers only have a
finite amount of memory, | 3,587.05 | 2.99 |
a finite number of transistors, a
finite number of light bulbs inside. | 3,590.04 | 3.47 |
So what happens when
you run out of space? | 3,593.51 | 3.84 |
In other words, if you
think back to last week | 3,597.35 | 2.27 |
when we talked about numbers
themselves being represented in binary, | 3,599.62 | 3.54 |
suppose that we've got
this 8-bit value here. | 3,603.16 | 2.32 |
>> And we have seven 1's and one 0. | 3,605.48 | 2.81 |
And suppose that we want
to add 1 to this value. | 3,608.29 | 2.537 |
This is a really big number right now. | 3,610.827 | 1.583 |
>> This is 254, if I remember
the math from last week right. | 3,612.41 | 4.2 |
But what if I change
that rightmost 0 to a 1? | 3,616.61 | 2.87 |
The whole number, of
course, becomes eight 1's. | 3,619.48 | 3.32 |
So we're still good. | 3,622.8 | 1.25 |
>> And that probably represents
255, though depending on context | 3,624.05 | 3.154 |
it could actually represent
a negative number. | 3,627.204 | 1.916 |
But more on that another time. | 3,629.12 | 2.12 |
This feels like it's about
as high as I can count. | 3,631.24 | 2.98 |
>> Now, it's only 8 bits. | 3,634.22 | 1.07 |
And my Mac, surely, has way
more than 8 bits of memory. | 3,635.29 | 2.88 |
But it does have finite. | 3,638.17 | 1 |
So the same argument applies, even if we
have more of these ones on the screen. | 3,639.17 | 4.06 |
>> But what happens if you're
storing this number, 255, | 3,643.23 | 3.79 |
and you want to count 1 bit higher? | 3,647.02 | 2.27 |
You want to go from 255 to 256. | 3,649.29 | 2.31 |
The problem, of course, is that if you
start counting at zero like last week, | 3,651.6 | 4.2 |
you can't count as high
as 256, let alone 257, | 3,655.8 | 3.87 |
let alone 258,m because what
happens when you add a 1? | 3,659.67 | 2.914 |
If you do the old grade school
approach, you put a 1 here, | 3,662.584 | 2.416 |
and then 1 plus 1 is 2, but that's
really a zero, you carry the 1, | 3,665 | 3.15 |
carry the 1, carry the 1. | 3,668.15 | 1.545 |
All of these things,
these 1's, go to zero. | 3,669.695 | 2.925 |
And you wind up, yes, as someone
pointed out, a 1 on the left hand side. | 3,672.62 | 5.2 |
But everything you can
actually see and fit in memory | 3,677.82 | 4.72 |
is just eight 0's, which is to say
at some point if you, a computer, | 3,682.54 | 5.42 |
tried counting high enough up, you're
going to wrap around, it would seem, | 3,687.96 | 4.53 |
to zero, or maybe even negative
numbers, which are even lower than zero. | 3,692.49 | 3.36 |
>> And we can kind of see this. | 3,695.85 | 1.41 |
Let me go ahead and write
a real quick program here. | 3,697.26 | 2.64 |
Let me go ahead and write
a program called Overflow. | 3,699.9 | 3.79 |
Include CS50.h, include
standard IO.h-- oh, | 3,703.69 | 6.29 |
I really missed my syntax highlighting. | 3,709.98 | 1.75 |
So let's save this as overflow.c. | 3,711.73 | 2.71 |
>> And now int main void--
and before long, we'll | 3,714.44 | 2.644 |
come back to explaining why
we keep writing int main void. | 3,717.084 | 2.416 |
But for now, let's just do
it, taking it for granted. | 3,719.5 | 2.58 |
Let's give myself an int,
and initialize it to 0. | 3,722.08 | 4.12 |
>> Let's then do for int i get zero--
actually, let's do an infinite loop | 3,726.2 | 5.516 |
and see what happens. | 3,731.716 | 0.874 |
While true, then let's print out n
is percent i, backslash n, plug-in n. | 3,732.59 | 9.85 |
But, now, let's do n gets n plus 1. | 3,742.44 | 4.76 |
>> So in other words, on each
iteration of this infinite loop, | 3,747.2 | 2.46 |
let's take n's value,
and add 1 to it, and then | 3,749.66 | 2.89 |
store the result back in n on the left. | 3,752.55 | 1.8 |
And, in fact, we've seen syntax
slightly like this, briefly. | 3,754.35 | 2.8 |
A cool trick is instead
of writing all this out, | 3,757.15 | 2.58 |
you can actually say an n plus equals 1. | 3,759.73 | 3.04 |
>> Or if you really want to be fancy,
you can say n plus plus semi-colon. | 3,762.77 | 4.71 |
But these latter two are just
what we'd call syntactic sugar | 3,767.48 | 2.65 |
for the first thing. | 3,770.13 | 0.66 |
>> The first thing is more explicit,
totally fine, totally correct. | 3,770.79 | 2.666 |
But this is more common, I'll say. | 3,773.456 | 2.014 |
So we'll do this for just a moment. | 3,775.47 | 1.74 |
>> Let's now make overflow, which sounds
rather ominous, dot slash overflow. | 3,777.21 | 4.475 |
Let's see, n's getting pretty big. | 3,784.38 | 5.472 |
But let's think, how big can n get? | 3,789.852 | 1.458 |
>> n is an int. | 3,791.31 | 1.56 |
We saw a moment ago with the size of
example that an int is four bytes. | 3,792.87 | 3.53 |
We know from last week, four bytes is
32 bits, because 8 times 4, that's 32. | 3,796.4 | 5.67 |
That's going to be 4 billion. | 3,802.07 | 1.39 |
>> And we are up to 800,000. | 3,803.46 | 2.342 |
This is going to take forever to
count as high as I possibly can. | 3,805.802 | 2.708 |
So I'm going to go ahead,
as you might before long, | 3,808.51 | 2.125 |
and hit Control C-- frankly, Control
C, a lot, where Control C generally | 3,810.635 | 4.275 |
means cancel. | 3,814.91 | 1.124 |
Unfortunately, because this
is running in the cloud, | 3,816.034 | 2.166 |
sometimes the cloud is
spitting out so much stuff, | 3,818.2 | 2.99 |
so much output, it's going to
take a little while for my input | 3,821.19 | 2.99 |
to get to the cloud. | 3,824.18 | 1.45 |
So even though I hit
Control C a few seconds ago, | 3,825.63 | 3.61 |
this is definitely the side
effect of an infinite loop. | 3,829.24 | 3.87 |
>> And so in such cases, we're
going to leave that be. | 3,833.11 | 2.96 |
And we're going to add another
terminal window over here | 3,836.07 | 2.98 |
with the plus, which of course doesn't
like that, since it's still thinking. | 3,839.05 | 4.136 |
And let's go ahead and be
a little more reasonable. | 3,843.186 | 2.124 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.