[CS61C FA20] Lecture 05.1 - C Memory Management: Dynamic Memory Allocation — Transcript
Full transcript
- 0:00and welcome back. Now, let's actually
- 0:03get real and explain to you how to
- 0:05actually work with real memory.
- 0:09So, this is what makes C different from
- 0:12Java and Python. You're going to have
- 0:14control of how much memory you get and
- 0:16and and how how much you want and how to
- 0:18release it and do all those things.
- 0:19Let's talk about it today. So, we
- 0:21learned that C has this size of uh
- 0:24function which gives the size and bytes
- 0:26of the thing you pass in. That's great.
- 0:28And that can be a type or it can be a
- 0:29variable which is nice. Um, and back
- 0:31back in the day ints were 16 bits. So
- 0:34you could ask size of int
- 0:3630 years ago, 40 years ago, 30 maybe,
- 0:39and it would say two and now it'll say
- 0:41four. Maybe now it'll say eight soon.
- 0:43Um,
- 0:45size this is again why you use intypes
- 0:47if I keep saying that. Um, so size of
- 0:49know the type of array. If I have uh int
- 0:51ar of three, size of ar says 12. Uh,
- 0:54that's three integers. And so 3 * 4 is
- 0:5712 as well as integers that you know at
- 0:59runtime. So you know here is a r of n
- 1:02which is a dynamic number. Uh or it's a
- 1:05function that returns three and you say
- 1:06int ar of some function whose eval which
- 1:09evaluates to three and a r of that which
- 1:11is three. Do it again. It'll still say
- 1:1312 for the size of number of bytes
- 1:15you've got.
- 1:17So this is the first time we've ever
- 1:19talked about this in this class. The way
- 1:21you ask for memory dynamically
- 1:23aside from the array. There's an array.
- 1:25We talked about it before is using Malo
- 1:27and we'll talk about what the difference
- 1:28between Malo and an array is soon. So
- 1:32here's a com here's the first time
- 1:34you've you've done this. So I say
- 1:35pointer I've got a pointer to an array
- 1:37of integers. I'll say array of integers.
- 1:39Um
- 1:41I declare it malik. So malo is a
- 1:44function that takes in uh the number of
- 1:47bytes you want and returns a pointer to
- 1:49uninitialized space. So it's not
- 1:51initialized. Remember C never
- 1:52initializes these for you. And it also
- 1:54returns it as a void star because it
- 1:55doesn't know what it wants in general.
- 1:56It's going to return a void star.
- 1:58Remember void star is a pointer to
- 1:59generic space to the generic array. You
- 2:02have to cast that. So you have to say
- 2:04this if pointer is declared an int star.
- 2:06So int star pointer you have to then
- 2:10cast that with this. And that tells the
- 2:12system don't worry about it. I know it
- 2:13read malic returns a void star but I
- 2:15want to cast it as an instar so that the
- 2:17equal sign matches up. The right side of
- 2:19the equals instar. The left side equals
- 2:21is an instar. So that's good. Okay.
- 2:23That's all is it's called a type cast.
- 2:24We've kind of seen that. That's that's
- 2:26the first time we've seen that actually.
- 2:28Malik, however, is almost never
- 2:29reserved. Why would you malic just to
- 2:31get one integer? If you just want
- 2:32integer, make it an int. Make a, you
- 2:33know, foo in foo, there's an integer.
- 2:35You probably want an array. You probably
- 2:36want a lot of them. Um, so this is the
- 2:38way it typically is done. Pointer equals
- 2:40instar malic and you have n times size
- 2:42of that. So that's a that says I want an
- 2:44array of n integers and pointer points
- 2:46to that array. Now, it's not a static
- 2:48array with the square brackets, but it
- 2:50is it is an array nevertheless. We still
- 2:51call them arrays.
- 2:54So
- 2:56once Malik is called, the memory
- 2:57location contains garbage. You know
- 2:59this, right? You know that we never
- 3:00initialize things. We never reset
- 3:01things. C is too fast. So that's going
- 3:04to be an uninitialized set of if it's
- 3:06three guys, three integers, it's three
- 3:08garbage integers. Just 12 bytes of
- 3:11nothing. Who knows what they are? The
- 3:13key is when you're done with that space,
- 3:16whatever pointer, whatever that malic
- 3:18returned, that's a value. That's a
- 3:20pointer to the beginning of that space.
- 3:22you eventually need to free it. And
- 3:24there's a kind of a contract. There's a
- 3:27contract between Malik and you. It's
- 3:30almost like I don't know. It's almost
- 3:31like going to the Godfather and asking
- 3:34for a favor.
- 3:39Godfather,
- 3:41I would like some some memory from you,
- 3:49some name.
- 3:51and that day may never come
- 3:54and may call upon you to do a vein upon
- 3:56me. But until that day, I will give you
- 3:59Zalik under the contract that you must
- 4:02free it when you are done.
- 4:05God by imitation. So even though the
- 4:08program is going to free it when you're
- 4:09done, don't do that. Don't let the
- 4:11program free it for you. Make sure you
- 4:12free yourself. Why? Because you never
- 4:15know when your main becomes a sub
- 4:17routine. They say, "Oh, you know, this
- 4:18is a pretty cool thing. Let me wrap it
- 4:19in a sub routine and you'll be here." by
- 4:20the way, but but you never freed it. So
- 4:22now this main never frees it. We call
- 4:23this a memory leak. And all of a sudden
- 4:25that's a sub routine. Now now you're
- 4:26going to call it many times and all of a
- 4:27sudden all the memory you ask for it
- 4:29just gets spilled. It doesn't get
- 4:31spilled, it gets uh leaked, gets leaked.
- 4:33It's a leak of memory and that's really
- 4:35bad. So you don't want that. So
- 4:37definitely listen to the Godfather and
- 4:38free it at the end when you're all done.
- 4:41And here's there's a lot of details to
- 4:42this. You can't use it after you free
- 4:44it. Once you free it, you're all done.
- 4:45So you whatever mallet gives you that
- 4:46pointer, you better pass that same
- 4:48pointer to free someday. Use it, use it,
- 4:51use it, then pass it to free and then
- 4:52you can't use it after that. Again,
- 4:54important thing. So here are some things
- 4:58that going to bite you. We're going to
- 4:59see this a lot because I want to make
- 4:59sure you see all these things that'll
- 5:00bite you because they will bite you.
- 5:01They bite. They bit me. They bit they
- 5:03basically bite every beginning
- 5:05programmer. The following two things
- 5:06will cause an error. Freeing the same
- 5:08piece of memory twice. I told you that
- 5:09once you free it once, you can't touch
- 5:10it again. It's it's out there and it can
- 5:12be reused maybe. So you can't free it
- 5:13twice. You also can't call free on
- 5:15something you didn't get back from
- 5:17Malak. So I got here's a Malik. You can
- 5:19only call free on that guy. You can't
- 5:20free on anything else. If you move the
- 5:21pointer, you can't call free on the new
- 5:23pointer. You have to call free on the
- 5:24exact address passed back from Malik.
- 5:27Okay. So kind of three things you can do
- 5:28wrong. If I told you Malik and then free
- 5:30that guy eventually after you're done
- 5:31with it. Well, you were done with it,
- 5:34but then you freed it twice. Bad. Or you
- 5:36you you called free and then on
- 5:39something that was not that. Or you
- 5:40didn't free at all. a lot of ways you
- 5:42can mess up and the runtime doesn't
- 5:43check for it. It doesn't check for this.
- 5:45Memor C is too fast. C is too
- 5:47performance critical. Doesn't do this.
- 5:48Your code's going to be either left in
- 5:50an inconsistent state because you've
- 5:51messed with the memory allocator somehow
- 5:53or and you won't find that bug until way
- 5:55later, which is bad. You're running a
- 5:56server and all of a sudden, hey, so why
- 5:57is the server crash? Yeah, because you
- 5:58did some weird thing with Malak and free
- 6:00back then and then eventually you paid
- 6:02for it later. Really bad. Kind of like
- 6:04no brush not brushing your teeth. Brush
- 6:06your teeth because years later you'll
- 6:07pay for it. Trust me, I'm fine with
- 6:09this. The point is that now you can
- 6:12turns out turns out okay you malic
- 6:14something big space okay but um h
- 6:18interesting interesting what if I wanted
- 6:20to make it bigger or smaller could I
- 6:24actually resize it and say sure so it's
- 6:27called realic real takes the pointer the
- 6:30original pointer that Malik gave you so
- 6:32you know return of guy from Malik is a
- 6:34pointer to that I could say you know
- 6:35what I I want more now I want twice as
- 6:37much or five times you know five times
- 6:39as much or I want it smaller. I can call
- 6:41real and that returns a new pointer.
- 6:43Now, what I didn't tell you, this is the
- 6:46thing on the previous slide. Malik
- 6:47sometimes can't satisfy your request.
- 6:50I'd like four trillion billion. I'd like
- 6:521 billion integers. I'd want some amount
- 6:54of space and maybe the space is run up.
- 6:56Maybe all the space you had access to is
- 6:58full. Malik has to have a failure mode.
- 7:02What's it? Let's think about it. What's
- 7:03its failure mode? Before I told you at
- 7:05the Unix level when you return a zero,
- 7:06it's success. Everything else is a
- 7:08failure. But here what is the sentinel
- 7:10for memory? What's the single value that
- 7:12Malik could return that we know could
- 7:14never be a val valid value that Malik
- 7:16could return? Zero. Null. So every call
- 7:21to Malik had better have the next line
- 7:24you check to see if the pointer equal
- 7:26equal null. Now one of the early
- 7:29mistakes people make is check if pointer
- 7:31equals null. Well pointer equals null
- 7:33assigns pointer to null. So now all of a
- 7:35sudden you've you've lost you've you've
- 7:39that's spilled. You've leaked the memory
- 7:41that Malik gave you. Malik gave you some
- 7:42memory. Then you just erased it. So now
- 7:43you have no way to get back and free it
- 7:44again. That was bad. Uh because you
- 7:46overwrote it by saying point equals
- 7:48null. And it's always going to be false
- 7:49even though Malik actually was
- 7:50successful. That's a buggy thing. So
- 7:52pointer equal equal. So it turns out
- 7:53what people do is they'll write null
- 7:55equal equal pointer because if you ever
- 7:57mistake it with one equal sign, null
- 7:59equal pointer doesn't make any sense and
- 8:01that'll be an error there. But null
- 8:02equal equal pointer actually works. So
- 8:04they'll flip it around. Really
- 8:05interesting. So you test if null equal
- 8:07equal pointer as a way to make sure that
- 8:09it's there. And if sorry null equal yeah
- 8:11null equal pointer and if that that's
- 8:13the case then you say error error I
- 8:14couldn't get memory. You gracefully
- 8:15handle it and you do something else. Say
- 8:17hey sorry you asked for a big you know
- 8:19some memory I couldn't give it to you.
- 8:21Uh otherwise otherwise you keep going
- 8:23and you got the memory you can now use
- 8:24it. So the line over here on the right
- 8:26says IP integer pointer equals this
- 8:29mallet guy. I ask for 10 integers and
- 8:31you always check for it equal equal to
- 8:32null after that call then I'm going to
- 8:34reallocate it say you know what I want
- 8:36to make I really I said 10 I said 20 I
- 8:39really meant 20 I meant 20 and so you
- 8:41reallocate all of a sudden it's 10 now
- 8:42it's 20 okay now the key is
- 8:46it is supposed to if it's bigger it is
- 8:48supposed to move the material over if it
- 8:51actually it might actually give you know
- 8:53I don't have 10 here but I have 20 over
- 8:55here I have 20 here but I have 20 over
- 8:56here so actually I'll go here and what
- 8:58it's supposed to
- 8:59If it all works, it's supposed to move
- 9:01the 10 over that. Whatever content you
- 9:03had in there, you filled it with 10
- 9:04values. 1 through 10. I move over here
- 9:06and now I've got 20 I can access. It's
- 9:08supposed to have moved the original 10
- 9:10over. So, it's actually pretty cool.
- 9:11They do it automatically. So, React is
- 9:13really nice, but check it. It might not
- 9:15have worked. So, make sure you check it.
- 9:17Make sure that the contents is first of
- 9:18all, check if it's null and also check
- 9:20if it did if it did the the the resize
- 9:22correctly. If it doesn't work, it's
- 9:24zero. It'll return null and it's there.
- 9:26If you want to free it, I don't
- 9:28recommend this. I would prefer you call
- 9:29free. You can say realic IP zero. It's
- 9:31the same as a free. It says, you know
- 9:32what? I don't want anything anymore.
- 9:34That's the same way as calling free. But
- 9:35I prefer to call free myself explicitly.
- 9:38Now, arrays are strange. Arrays are not
- 9:40implemented as you'd think. Let me get
- 9:42my let me get my pen here and make sure
- 9:44I've got this going. So, here's a piece
- 9:46of code. FU. Let's And here's an array.
- 9:50First line. Okay, here we go. int
- 9:55star p star q and x. x is an integer.
- 9:57What's its contents? Garbage. P and q
- 10:00are pointers. What's their contents?
- 10:01Garbage. When pointers have garbage
- 10:03contents, we draw we draw their pointers
- 10:05as kind of pointing who knows where.
- 10:07This is like who knows where. I don't
- 10:09know. Here is int a of four.
- 10:13Now, where does a live? I don't know.
- 10:15See, arrays are not implemented like you
- 10:17think they would be. Okay. But a of four
- 10:20says here's four integers I'm g give you
- 10:23and they're all contiguous. We know
- 10:24they're always continuous. Malik returns
- 10:26a contiguous memory and so does A of
- 10:29four. Okay. So now here we go. Let me
- 10:32let me clear this here. Next line. Next
- 10:35line.
- 10:37P equals there's like the three ways to
- 10:39do this now. Well, I mean there's the
- 10:41raw way to say int x. Here's a of four.
- 10:45That's an array way. And now there's the
- 10:46malic way. I'm showing you this one.
- 10:48This says P equals instar malic size
- 10:50event just makes one int. Again, usually
- 10:51don't call it usually say n times size
- 10:53int or some size like that. So I make a
- 10:55new integer. So what happens
- 10:59over here
- 11:02is the result of malik. Okay, that's the
- 11:05result of malik. I made some new space.
- 11:06I made an integer and
- 11:09p is going to point to it. When when
- 11:12something points to something, its
- 11:13address here 40 is the value of the
- 11:16pointer. So 4040 makes sense. Okay. So P
- 11:19points to that Malik integer over there.
- 11:22Now Q equals address of X. So now Q is
- 11:25going to point to X. X doesn't have a
- 11:27value yet, but it points to it. Okay.
- 11:30So I've got these three uninitialized
- 11:33integers. One, two, and three. Let's see
- 11:35what actually is going to happen. Here
- 11:37we go.
- 11:40Star P equals 1.
- 11:43Okay. I follow the pointer. I follow the
- 11:46pointer. I stuff a one there. That's as
- 11:48a result of that line star P equals 1.
- 11:50Okay. And so now if I ask for P
- 11:57sorry star P and address of P, what
- 12:00should it give me? Star P. Follow it.
- 12:04Okay. P. There's P. What's address of P?
- 12:10Okay. So let's see what it prints out.
- 12:11Ready? Boop. Start P1. P is 40. Address
- 12:15be 12. Makes sense, right? Thumbs up.
- 12:18Now, next line. Star Q equals 2.
- 12:21Remember, this guy was uninitialized
- 12:23still. Star Q equals 2. There we go. And
- 12:27now, let's ask for star Q
- 12:30and address of Q. Okay. Here we go.
- 12:35Ready? Star Q two Q 20 address of Q 16
- 12:442 and 16. Any questions?
- 12:47Pretty good, right? I'll wait. Feel free
- 12:50to ask your question.
- 12:52Too used to doing this dynamically. This
- 12:54video thing is very strange, by the way.
- 12:55I just need to say doing this in front
- 12:57of a camera is very strange. Finally,
- 12:59star A is three. Let's see what happens.
- 13:02Ready?Oop.
- 13:03Okay, so what A was pointing to is now
- 13:05three. Here we go. Star A. A. Address of
- 13:10A. Let's do it. Ready? Here we go. Star
- 13:12A. We know it's three.
- 13:16A. Well, we got to have 24 because
- 13:18obviously that's what it was. That
- 13:19that's what is there. Where does A live?
- 13:22What's address of A? Let's just see
- 13:24this. It's a little strange. Ready?
- 13:27Hear that glass breaking sound?
- 13:30How is the address of A 24? How does
- 13:33that make any sense?
- 13:37That's not address of A isn't 24. This
- 13:39is an A. That's not doesn't make any. So
- 13:42A is 24, right? A A the value A that
- 13:46points to 24. So this guy has 24 inside
- 13:48of it. But the address where does a live
- 13:51that's not 24. The reason is arrays are
- 13:54not implemented as you think. Or as KN&R
- 13:57says, an array name is not a variable.
- 14:00You're going to see when we get to the
- 14:02uh assembly level why that is. Remember
- 14:05this. hold this weird uncomfortable
- 14:07feeling right now because when we get to
- 14:09assembly you're going to see oh now I
- 14:11know remember that slide Dan showed me
- 14:13now I know why that is so mini summary
- 14:17for this little mini lecture here
- 14:19pointers and arrays are virtually the
- 14:21same except for that little exception I
- 14:23showed you about there about where they
- 14:24live and that you can't increment an
- 14:26array open square bracket variable C
- 14:29knows and increment pointers plus and
- 14:31minus it knows what the size of is and
- 14:32moves them around it's an efficient
- 14:34language with little protection ction.
- 14:36It's basically going to let you uh hurt
- 14:39yourself. It's a very sharpedged car.
- 14:41Every sharpedged car, car with no
- 14:42plastic around you, just a wild engine
- 14:45running there. Use handles to change
- 14:47pointers. I said that. Uh we saw the
- 14:49Godfather where you can use Malak and
- 14:51free, but you got to promise to return
- 14:52and free them, the stuff you borrow from
- 14:55the memory you borrow using Malik. And
- 15:00you get something, you know, ain't no
- 15:02free lunch, folks. Ain't no free lunch.
- 15:03What do you get for that speed? What you
- 15:05get for that speed is a lot of rope.
- 15:07That speed is remarkable, but a lot of
- 15:09rope and you can hang yourself with it.
- 15:11So, don't know about all these gotchas
- 15:12and try to avoid them. See the nice
- 15:15lecture.
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 05.1 - C Memory Management: Dynamic Memory Allocation by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 3,112 words across 425 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.