text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
or as we saw last time using pointer arithmetic,
487.12
3.46
actually using the star operator and maybe adding some number two
490.58
3.29
and address to get at some subsequent address.
493.87
3.44
But it turns out there's a few problems with this fundamental approach.
497.31
4.01
Nice and as simple as it is, it would seem that we rather paint ourselves
501.32
4.13
into a corner with this approach.
505.45
1.96
This array has 1, 2, 3, 4, 5, 6 total elements, at least as depicted here.
507.41
5.86
So that's fine if you want to insert a number, and then
513.27
2.36
another number, and then four more numbers.
515.63
1.791
But what if you want to then insert a seventh number,
517.421
2.636
not to mention an eighth number or a ninth number or the like?
520.057
2.583
Well, where do you put them?
522.64
1.14
Well, you might think, well, that's fine.
523.78
1.708
I'm just going to go put the seventh number over here,
525.488
2.732
or the eighth number over here, or the ninth number over there.
528.22
3.37
But you can't just blindly do that.
531.59
3.1
If this memory is being managed not by you
534.69
2.19
per se but by malloc and by the computer itself inside-- and your program,
536.88
4.25
this memory over here, while it might physically exist,
541.13
2.79
might be used by some other part of your program all together.
543.92
4.065
It doesn't necessarily belong to you unless you've asked for it.
547.985
2.775
And the problem with an array is that as we've seen it typically
550.76
3.14
you declare their size in advance, as with the square bracket notation,
553.9
3.2
and say give me six integers or give me six something or others, but that's it.
557.1
4.42
You have to decide in advance.
561.52
1.58
You can't just grow it as you can in some programming languages thereafter.
563.1
3.67
You've rather painted yourself into a corner.
566.77
2.54
But with malloc and other functions like we saw last time,
569.31
3.77
you can actually allocate more memory using malloc.
573.08
3.24
Unfortunately, it might end up in another location in your computer's
576.32
3.897
memory, so you might have to do some copying
580.217
1.833
to take the original six elements and move them
582.05
1.75
elsewhere just to make room for more.
583.8
1.57
And there is a data function for that, something called [? re-alloc ?]
585.37
2.916
or reallocate.
588.286
0.884
And indeed it can do exactly that.
589.17
1.56
It can give you a bigger chunk of memory and reallocate
590.73
2.84
what was previously there to be a larger [? size. ?]
593.57
2.864
But you have to do a little bit of work.
596.434
1.666
You have to invoke it in order achieve that.
598.1
1.833
You can't just blindly keep adding things at the end of this array.
599.933
3.327
Now, unfortunately, while a solution that might not be very efficient.
603.26
4.46
Even if you can allocate a bigger chunk of memory that's bigger than six
607.72
3.841
because you have more numbers, for instance, to store,
611.561
2.249
what if that takes a bit of time?
613.81
3.059
And indeed it's going to.
616.869
1.041
If you allocate more integers somewhere else in memory
617.91
2.52
you still have to copy those original values,
620.43
1.93
and now it just feels like you're wasting time.
622.36
1.97
Now, instead of just inserting things into the list,
624.33
2.18
you might have to copy it into a bigger space, reallocate things, grow.
626.51
4.01
It's a lot more work.
630.52
0.9
And all of that discussion of running time and performance
631.42
3.22
comes back into play, because if that whole copying process and reallocating
634.64
4.02
is costing you time, your algorithm or your program
638.66
2.86
ultimately might not really be as fast as you might want it.
641.52
3.73
So, what could we do instead?
645.25
1.46
What could we do instead in order to solve
646.71
2.54
this problem dynamically, so to speak, that being the operative word.
649.25
3.81
And luckily enough, last week we learned that there is dynamic memory allocation
653.06
4.86
in C by way of that function malloc.
657.92
2.2
And we also learned that there is ways of representing structures
660.12
3.21
in C that you don't necessarily get with the language itself,
663.33
3.949
because they're not primitives.
667.279
1.291
They're not built in.
668.57
0.96
In other words, let me propose this as a solution to our problem.
669.53
4.35
This is a list of, let's see, five numbers it would seem,
673.88
4.14
9, 17, 22, 26, and 34.
678.02
4.16
Pretty arbitrary right now, but you might
682.18
2.16
imagine very simply drawing those same numbers-- 9, 17, 22, 26,
684.34
3.51
34-- in the form of an array and they're clearly deliberately sorted.
687.85
5.37
But again, what if you wanted to grow that array or even shrink that array
693.22
3.87
dynamically over time?
697.09
1.76
Well, let me propose that we not draw those numbers back to back to back
698.85
3.52
to back literally next to each other but allow ourselves potentially
702.37
3.58
a little bit of space?
705.95
1.65
But if that's the case and nine is here in my computer's memory and 17 is here
707.6
4.32
and 22 is here, or over here, or over here-- in other words,
711.92
3.09
what if I relax the constraint that my numbers or my data types more
715.01
3.43
generally have to be stored contiguously back to back to back to back in memory
718.44
4.27
and instead allow them to be anywhere, indeed anywhere a function like malloc
722.71
3.6
wants to give me more memory, that's fine.
726.31
2.12
If it wants to give me memory up here in my computer, I'll deal with that.
728.43
2.53
If it wants to give me extra memory over here, that's fine.
730.96
2.458
I'll deal with it, because I'll use these conceptual arrows to stitch
733.418
4.092
together my data structure this time.
737.51
3.096
And now, where have we seen these kinds of arrows before?
740.606
2.374
What feature of C allows us to connect one thing
742.98
3.64
to another where a la chutes and ladders get from one place to another?
746.62
4.48
Well, that's exactly what we saw last time which was pointers.
751.1
3.13
While we've drawn these here per the snippet from a textbook using arrows,
754.23
4.67
those are really just pointers.
758.9
1.51
And what does each of these rectangles represent?
760.41
3.22
Well, clearly a number in the top half of the rectangle,
763.63
3.07
but I claim that at the bottom half of these rectangles
766.7
3.11
let's consider that bottom rectangle to just be another piece of data,
769.81
3.22
specifically an int star, a pointer.
773.03
2.79
Or rather not a pointer because it seems to be pointing not just to the number
775.82
4.13
but to this whole rectangle, so I need some new terminology.
779.95
3.37
I need some kind of structure to contain an integer and this pointer.
783.32
5.58