YouTube2Text

[CS61C FA20] Lecture 05.1 - C Memory Management: Dynamic Memory Allocation — Transcript

by CS 61C Departmental · 3,112 words · 425 segments · language en · Watch on YouTube

Full transcript

  1. 0:00and welcome back. Now, let's actually
  2. 0:03get real and explain to you how to
  3. 0:05actually work with real memory.
  4. 0:09So, this is what makes C different from
  5. 0:12Java and Python. You're going to have
  6. 0:14control of how much memory you get and
  7. 0:16and and how how much you want and how to
  8. 0:18release it and do all those things.
  9. 0:19Let's talk about it today. So, we
  10. 0:21learned that C has this size of uh
  11. 0:24function which gives the size and bytes
  12. 0:26of the thing you pass in. That's great.
  13. 0:28And that can be a type or it can be a
  14. 0:29variable which is nice. Um, and back
  15. 0:31back in the day ints were 16 bits. So
  16. 0:34you could ask size of int
  17. 0:3630 years ago, 40 years ago, 30 maybe,
  18. 0:39and it would say two and now it'll say
  19. 0:41four. Maybe now it'll say eight soon.
  20. 0:43Um,
  21. 0:45size this is again why you use intypes
  22. 0:47if I keep saying that. Um, so size of
  23. 0:49know the type of array. If I have uh int
  24. 0:51ar of three, size of ar says 12. Uh,
  25. 0:54that's three integers. And so 3 * 4 is
  26. 0:5712 as well as integers that you know at
  27. 0:59runtime. So you know here is a r of n
  28. 1:02which is a dynamic number. Uh or it's a
  29. 1:05function that returns three and you say
  30. 1:06int ar of some function whose eval which
  31. 1:09evaluates to three and a r of that which
  32. 1:11is three. Do it again. It'll still say
  33. 1:1312 for the size of number of bytes
  34. 1:15you've got.
  35. 1:17So this is the first time we've ever
  36. 1:19talked about this in this class. The way
  37. 1:21you ask for memory dynamically
  38. 1:23aside from the array. There's an array.
  39. 1:25We talked about it before is using Malo
  40. 1:27and we'll talk about what the difference
  41. 1:28between Malo and an array is soon. So
  42. 1:32here's a com here's the first time
  43. 1:34you've you've done this. So I say
  44. 1:35pointer I've got a pointer to an array
  45. 1:37of integers. I'll say array of integers.
  46. 1:39Um
  47. 1:41I declare it malik. So malo is a
  48. 1:44function that takes in uh the number of
  49. 1:47bytes you want and returns a pointer to
  50. 1:49uninitialized space. So it's not
  51. 1:51initialized. Remember C never
  52. 1:52initializes these for you. And it also
  53. 1:54returns it as a void star because it
  54. 1:55doesn't know what it wants in general.
  55. 1:56It's going to return a void star.
  56. 1:58Remember void star is a pointer to
  57. 1:59generic space to the generic array. You
  58. 2:02have to cast that. So you have to say
  59. 2:04this if pointer is declared an int star.
  60. 2:06So int star pointer you have to then
  61. 2:10cast that with this. And that tells the
  62. 2:12system don't worry about it. I know it
  63. 2:13read malic returns a void star but I
  64. 2:15want to cast it as an instar so that the
  65. 2:17equal sign matches up. The right side of
  66. 2:19the equals instar. The left side equals
  67. 2:21is an instar. So that's good. Okay.
  68. 2:23That's all is it's called a type cast.
  69. 2:24We've kind of seen that. That's that's
  70. 2:26the first time we've seen that actually.
  71. 2:28Malik, however, is almost never
  72. 2:29reserved. Why would you malic just to
  73. 2:31get one integer? If you just want
  74. 2:32integer, make it an int. Make a, you
  75. 2:33know, foo in foo, there's an integer.
  76. 2:35You probably want an array. You probably
  77. 2:36want a lot of them. Um, so this is the
  78. 2:38way it typically is done. Pointer equals
  79. 2:40instar malic and you have n times size
  80. 2:42of that. So that's a that says I want an
  81. 2:44array of n integers and pointer points
  82. 2:46to that array. Now, it's not a static
  83. 2:48array with the square brackets, but it
  84. 2:50is it is an array nevertheless. We still
  85. 2:51call them arrays.
  86. 2:54So
  87. 2:56once Malik is called, the memory
  88. 2:57location contains garbage. You know
  89. 2:59this, right? You know that we never
  90. 3:00initialize things. We never reset
  91. 3:01things. C is too fast. So that's going
  92. 3:04to be an uninitialized set of if it's
  93. 3:06three guys, three integers, it's three
  94. 3:08garbage integers. Just 12 bytes of
  95. 3:11nothing. Who knows what they are? The
  96. 3:13key is when you're done with that space,
  97. 3:16whatever pointer, whatever that malic
  98. 3:18returned, that's a value. That's a
  99. 3:20pointer to the beginning of that space.
  100. 3:22you eventually need to free it. And
  101. 3:24there's a kind of a contract. There's a
  102. 3:27contract between Malik and you. It's
  103. 3:30almost like I don't know. It's almost
  104. 3:31like going to the Godfather and asking
  105. 3:34for a favor.
  106. 3:39Godfather,
  107. 3:41I would like some some memory from you,
  108. 3:49some name.
  109. 3:51and that day may never come
  110. 3:54and may call upon you to do a vein upon
  111. 3:56me. But until that day, I will give you
  112. 3:59Zalik under the contract that you must
  113. 4:02free it when you are done.
  114. 4:05God by imitation. So even though the
  115. 4:08program is going to free it when you're
  116. 4:09done, don't do that. Don't let the
  117. 4:11program free it for you. Make sure you
  118. 4:12free yourself. Why? Because you never
  119. 4:15know when your main becomes a sub
  120. 4:17routine. They say, "Oh, you know, this
  121. 4:18is a pretty cool thing. Let me wrap it
  122. 4:19in a sub routine and you'll be here." by
  123. 4:20the way, but but you never freed it. So
  124. 4:22now this main never frees it. We call
  125. 4:23this a memory leak. And all of a sudden
  126. 4:25that's a sub routine. Now now you're
  127. 4:26going to call it many times and all of a
  128. 4:27sudden all the memory you ask for it
  129. 4:29just gets spilled. It doesn't get
  130. 4:31spilled, it gets uh leaked, gets leaked.
  131. 4:33It's a leak of memory and that's really
  132. 4:35bad. So you don't want that. So
  133. 4:37definitely listen to the Godfather and
  134. 4:38free it at the end when you're all done.
  135. 4:41And here's there's a lot of details to
  136. 4:42this. You can't use it after you free
  137. 4:44it. Once you free it, you're all done.
  138. 4:45So you whatever mallet gives you that
  139. 4:46pointer, you better pass that same
  140. 4:48pointer to free someday. Use it, use it,
  141. 4:51use it, then pass it to free and then
  142. 4:52you can't use it after that. Again,
  143. 4:54important thing. So here are some things
  144. 4:58that going to bite you. We're going to
  145. 4:59see this a lot because I want to make
  146. 4:59sure you see all these things that'll
  147. 5:00bite you because they will bite you.
  148. 5:01They bite. They bit me. They bit they
  149. 5:03basically bite every beginning
  150. 5:05programmer. The following two things
  151. 5:06will cause an error. Freeing the same
  152. 5:08piece of memory twice. I told you that
  153. 5:09once you free it once, you can't touch
  154. 5:10it again. It's it's out there and it can
  155. 5:12be reused maybe. So you can't free it
  156. 5:13twice. You also can't call free on
  157. 5:15something you didn't get back from
  158. 5:17Malak. So I got here's a Malik. You can
  159. 5:19only call free on that guy. You can't
  160. 5:20free on anything else. If you move the
  161. 5:21pointer, you can't call free on the new
  162. 5:23pointer. You have to call free on the
  163. 5:24exact address passed back from Malik.
  164. 5:27Okay. So kind of three things you can do
  165. 5:28wrong. If I told you Malik and then free
  166. 5:30that guy eventually after you're done
  167. 5:31with it. Well, you were done with it,
  168. 5:34but then you freed it twice. Bad. Or you
  169. 5:36you you called free and then on
  170. 5:39something that was not that. Or you
  171. 5:40didn't free at all. a lot of ways you
  172. 5:42can mess up and the runtime doesn't
  173. 5:43check for it. It doesn't check for this.
  174. 5:45Memor C is too fast. C is too
  175. 5:47performance critical. Doesn't do this.
  176. 5:48Your code's going to be either left in
  177. 5:50an inconsistent state because you've
  178. 5:51messed with the memory allocator somehow
  179. 5:53or and you won't find that bug until way
  180. 5:55later, which is bad. You're running a
  181. 5:56server and all of a sudden, hey, so why
  182. 5:57is the server crash? Yeah, because you
  183. 5:58did some weird thing with Malak and free
  184. 6:00back then and then eventually you paid
  185. 6:02for it later. Really bad. Kind of like
  186. 6:04no brush not brushing your teeth. Brush
  187. 6:06your teeth because years later you'll
  188. 6:07pay for it. Trust me, I'm fine with
  189. 6:09this. The point is that now you can
  190. 6:12turns out turns out okay you malic
  191. 6:14something big space okay but um h
  192. 6:18interesting interesting what if I wanted
  193. 6:20to make it bigger or smaller could I
  194. 6:24actually resize it and say sure so it's
  195. 6:27called realic real takes the pointer the
  196. 6:30original pointer that Malik gave you so
  197. 6:32you know return of guy from Malik is a
  198. 6:34pointer to that I could say you know
  199. 6:35what I I want more now I want twice as
  200. 6:37much or five times you know five times
  201. 6:39as much or I want it smaller. I can call
  202. 6:41real and that returns a new pointer.
  203. 6:43Now, what I didn't tell you, this is the
  204. 6:46thing on the previous slide. Malik
  205. 6:47sometimes can't satisfy your request.
  206. 6:50I'd like four trillion billion. I'd like
  207. 6:521 billion integers. I'd want some amount
  208. 6:54of space and maybe the space is run up.
  209. 6:56Maybe all the space you had access to is
  210. 6:58full. Malik has to have a failure mode.
  211. 7:02What's it? Let's think about it. What's
  212. 7:03its failure mode? Before I told you at
  213. 7:05the Unix level when you return a zero,
  214. 7:06it's success. Everything else is a
  215. 7:08failure. But here what is the sentinel
  216. 7:10for memory? What's the single value that
  217. 7:12Malik could return that we know could
  218. 7:14never be a val valid value that Malik
  219. 7:16could return? Zero. Null. So every call
  220. 7:21to Malik had better have the next line
  221. 7:24you check to see if the pointer equal
  222. 7:26equal null. Now one of the early
  223. 7:29mistakes people make is check if pointer
  224. 7:31equals null. Well pointer equals null
  225. 7:33assigns pointer to null. So now all of a
  226. 7:35sudden you've you've lost you've you've
  227. 7:39that's spilled. You've leaked the memory
  228. 7:41that Malik gave you. Malik gave you some
  229. 7:42memory. Then you just erased it. So now
  230. 7:43you have no way to get back and free it
  231. 7:44again. That was bad. Uh because you
  232. 7:46overwrote it by saying point equals
  233. 7:48null. And it's always going to be false
  234. 7:49even though Malik actually was
  235. 7:50successful. That's a buggy thing. So
  236. 7:52pointer equal equal. So it turns out
  237. 7:53what people do is they'll write null
  238. 7:55equal equal pointer because if you ever
  239. 7:57mistake it with one equal sign, null
  240. 7:59equal pointer doesn't make any sense and
  241. 8:01that'll be an error there. But null
  242. 8:02equal equal pointer actually works. So
  243. 8:04they'll flip it around. Really
  244. 8:05interesting. So you test if null equal
  245. 8:07equal pointer as a way to make sure that
  246. 8:09it's there. And if sorry null equal yeah
  247. 8:11null equal pointer and if that that's
  248. 8:13the case then you say error error I
  249. 8:14couldn't get memory. You gracefully
  250. 8:15handle it and you do something else. Say
  251. 8:17hey sorry you asked for a big you know
  252. 8:19some memory I couldn't give it to you.
  253. 8:21Uh otherwise otherwise you keep going
  254. 8:23and you got the memory you can now use
  255. 8:24it. So the line over here on the right
  256. 8:26says IP integer pointer equals this
  257. 8:29mallet guy. I ask for 10 integers and
  258. 8:31you always check for it equal equal to
  259. 8:32null after that call then I'm going to
  260. 8:34reallocate it say you know what I want
  261. 8:36to make I really I said 10 I said 20 I
  262. 8:39really meant 20 I meant 20 and so you
  263. 8:41reallocate all of a sudden it's 10 now
  264. 8:42it's 20 okay now the key is
  265. 8:46it is supposed to if it's bigger it is
  266. 8:48supposed to move the material over if it
  267. 8:51actually it might actually give you know
  268. 8:53I don't have 10 here but I have 20 over
  269. 8:55here I have 20 here but I have 20 over
  270. 8:56here so actually I'll go here and what
  271. 8:58it's supposed to
  272. 8:59If it all works, it's supposed to move
  273. 9:01the 10 over that. Whatever content you
  274. 9:03had in there, you filled it with 10
  275. 9:04values. 1 through 10. I move over here
  276. 9:06and now I've got 20 I can access. It's
  277. 9:08supposed to have moved the original 10
  278. 9:10over. So, it's actually pretty cool.
  279. 9:11They do it automatically. So, React is
  280. 9:13really nice, but check it. It might not
  281. 9:15have worked. So, make sure you check it.
  282. 9:17Make sure that the contents is first of
  283. 9:18all, check if it's null and also check
  284. 9:20if it did if it did the the the resize
  285. 9:22correctly. If it doesn't work, it's
  286. 9:24zero. It'll return null and it's there.
  287. 9:26If you want to free it, I don't
  288. 9:28recommend this. I would prefer you call
  289. 9:29free. You can say realic IP zero. It's
  290. 9:31the same as a free. It says, you know
  291. 9:32what? I don't want anything anymore.
  292. 9:34That's the same way as calling free. But
  293. 9:35I prefer to call free myself explicitly.
  294. 9:38Now, arrays are strange. Arrays are not
  295. 9:40implemented as you'd think. Let me get
  296. 9:42my let me get my pen here and make sure
  297. 9:44I've got this going. So, here's a piece
  298. 9:46of code. FU. Let's And here's an array.
  299. 9:50First line. Okay, here we go. int
  300. 9:55star p star q and x. x is an integer.
  301. 9:57What's its contents? Garbage. P and q
  302. 10:00are pointers. What's their contents?
  303. 10:01Garbage. When pointers have garbage
  304. 10:03contents, we draw we draw their pointers
  305. 10:05as kind of pointing who knows where.
  306. 10:07This is like who knows where. I don't
  307. 10:09know. Here is int a of four.
  308. 10:13Now, where does a live? I don't know.
  309. 10:15See, arrays are not implemented like you
  310. 10:17think they would be. Okay. But a of four
  311. 10:20says here's four integers I'm g give you
  312. 10:23and they're all contiguous. We know
  313. 10:24they're always continuous. Malik returns
  314. 10:26a contiguous memory and so does A of
  315. 10:29four. Okay. So now here we go. Let me
  316. 10:32let me clear this here. Next line. Next
  317. 10:35line.
  318. 10:37P equals there's like the three ways to
  319. 10:39do this now. Well, I mean there's the
  320. 10:41raw way to say int x. Here's a of four.
  321. 10:45That's an array way. And now there's the
  322. 10:46malic way. I'm showing you this one.
  323. 10:48This says P equals instar malic size
  324. 10:50event just makes one int. Again, usually
  325. 10:51don't call it usually say n times size
  326. 10:53int or some size like that. So I make a
  327. 10:55new integer. So what happens
  328. 10:59over here
  329. 11:02is the result of malik. Okay, that's the
  330. 11:05result of malik. I made some new space.
  331. 11:06I made an integer and
  332. 11:09p is going to point to it. When when
  333. 11:12something points to something, its
  334. 11:13address here 40 is the value of the
  335. 11:16pointer. So 4040 makes sense. Okay. So P
  336. 11:19points to that Malik integer over there.
  337. 11:22Now Q equals address of X. So now Q is
  338. 11:25going to point to X. X doesn't have a
  339. 11:27value yet, but it points to it. Okay.
  340. 11:30So I've got these three uninitialized
  341. 11:33integers. One, two, and three. Let's see
  342. 11:35what actually is going to happen. Here
  343. 11:37we go.
  344. 11:40Star P equals 1.
  345. 11:43Okay. I follow the pointer. I follow the
  346. 11:46pointer. I stuff a one there. That's as
  347. 11:48a result of that line star P equals 1.
  348. 11:50Okay. And so now if I ask for P
  349. 11:57sorry star P and address of P, what
  350. 12:00should it give me? Star P. Follow it.
  351. 12:04Okay. P. There's P. What's address of P?
  352. 12:10Okay. So let's see what it prints out.
  353. 12:11Ready? Boop. Start P1. P is 40. Address
  354. 12:15be 12. Makes sense, right? Thumbs up.
  355. 12:18Now, next line. Star Q equals 2.
  356. 12:21Remember, this guy was uninitialized
  357. 12:23still. Star Q equals 2. There we go. And
  358. 12:27now, let's ask for star Q
  359. 12:30and address of Q. Okay. Here we go.
  360. 12:35Ready? Star Q two Q 20 address of Q 16
  361. 12:442 and 16. Any questions?
  362. 12:47Pretty good, right? I'll wait. Feel free
  363. 12:50to ask your question.
  364. 12:52Too used to doing this dynamically. This
  365. 12:54video thing is very strange, by the way.
  366. 12:55I just need to say doing this in front
  367. 12:57of a camera is very strange. Finally,
  368. 12:59star A is three. Let's see what happens.
  369. 13:02Ready?Oop.
  370. 13:03Okay, so what A was pointing to is now
  371. 13:05three. Here we go. Star A. A. Address of
  372. 13:10A. Let's do it. Ready? Here we go. Star
  373. 13:12A. We know it's three.
  374. 13:16A. Well, we got to have 24 because
  375. 13:18obviously that's what it was. That
  376. 13:19that's what is there. Where does A live?
  377. 13:22What's address of A? Let's just see
  378. 13:24this. It's a little strange. Ready?
  379. 13:27Hear that glass breaking sound?
  380. 13:30How is the address of A 24? How does
  381. 13:33that make any sense?
  382. 13:37That's not address of A isn't 24. This
  383. 13:39is an A. That's not doesn't make any. So
  384. 13:42A is 24, right? A A the value A that
  385. 13:46points to 24. So this guy has 24 inside
  386. 13:48of it. But the address where does a live
  387. 13:51that's not 24. The reason is arrays are
  388. 13:54not implemented as you think. Or as KN&R
  389. 13:57says, an array name is not a variable.
  390. 14:00You're going to see when we get to the
  391. 14:02uh assembly level why that is. Remember
  392. 14:05this. hold this weird uncomfortable
  393. 14:07feeling right now because when we get to
  394. 14:09assembly you're going to see oh now I
  395. 14:11know remember that slide Dan showed me
  396. 14:13now I know why that is so mini summary
  397. 14:17for this little mini lecture here
  398. 14:19pointers and arrays are virtually the
  399. 14:21same except for that little exception I
  400. 14:23showed you about there about where they
  401. 14:24live and that you can't increment an
  402. 14:26array open square bracket variable C
  403. 14:29knows and increment pointers plus and
  404. 14:31minus it knows what the size of is and
  405. 14:32moves them around it's an efficient
  406. 14:34language with little protection ction.
  407. 14:36It's basically going to let you uh hurt
  408. 14:39yourself. It's a very sharpedged car.
  409. 14:41Every sharpedged car, car with no
  410. 14:42plastic around you, just a wild engine
  411. 14:45running there. Use handles to change
  412. 14:47pointers. I said that. Uh we saw the
  413. 14:49Godfather where you can use Malak and
  414. 14:51free, but you got to promise to return
  415. 14:52and free them, the stuff you borrow from
  416. 14:55the memory you borrow using Malik. And
  417. 15:00you get something, you know, ain't no
  418. 15:02free lunch, folks. Ain't no free lunch.
  419. 15:03What do you get for that speed? What you
  420. 15:05get for that speed is a lot of rope.
  421. 15:07That speed is remarkable, but a lot of
  422. 15:09rope and you can hang yourself with it.
  423. 15:11So, don't know about all these gotchas
  424. 15:12and try to avoid them. See the nice
  425. 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.