YouTube2Text

[CS61C FA20] Lecture 05.5 - C Memory Management: When Memory Goes Bad — Transcript

by CS 61C Departmental · 2,098 words · 326 segments · language en · Watch on YouTube

Full transcript

  1. 0:00and welcome back this last
  2. 0:03segment this last video on introduction
  3. 0:05to c
  4. 0:06the goal is to figure out how to teach
  5. 0:08you the things that you probably will do
  6. 0:10if we didn't tell you in advance
  7. 0:12these are the common mistakes people
  8. 0:13make when working with memory or
  9. 0:15dun dun dun when memory goes bad so this
  10. 0:18is just a review slide
  11. 0:19why do we do pointers at all well if i'm
  12. 0:21going to pass some value into a function
  13. 0:23i don't want to pass all the data it's
  14. 0:24via copy because it's called by value
  15. 0:26i want to pass a pointer so you can have
  16. 0:28access to my structure here my
  17. 0:30array or my big struct or something what
  18. 0:32are the drawbacks you've got to deal
  19. 0:33with these pointers you got to deal with
  20. 0:35the fact that when you make a malik and
  21. 0:36free request
  22. 0:37maybe you forget to do it maybe you're
  23. 0:39in the wrong order maybe you move the
  24. 0:40pointer around and you don't do it
  25. 0:41a lot of problems so dangly reference
  26. 0:43says i make a pointer i
  27. 0:44initialize a pointer then here is when
  28. 0:46i'm going to malik and set it to it but
  29. 0:48i start using it before
  30. 0:49i actually have set it to the right
  31. 0:51malik value
  32. 0:52there's another mini error of what
  33. 0:54happens when you start reading from this
  34. 0:55uninitialized array before you even
  35. 0:57write to it so that's an issue
  36. 0:59memory leak happens where you've
  37. 1:00reserved something from malik but
  38. 1:03then you never free it so a tardy freeze
  39. 1:05says you forget to free it
  40. 1:06you can also lose the pointer or you
  41. 1:08free it a little too late bad things can
  42. 1:09happen so
  43. 1:10free at the right time so this is the
  44. 1:12easiest case writing off the end of an
  45. 1:14array
  46. 1:14so here's foo it makes a hundred uh i'll
  47. 1:17i'll use a little picture here
  48. 1:19so it makes a little array i'll just
  49. 1:20write it that's my array and that pretty
  50. 1:22array
  51. 1:22and foo points to the beginning that's
  52. 1:24the zero guy there okay
  53. 1:26and at zero up to it's 100 elements so
  54. 1:28zero up to 99.
  55. 1:29well what does this loop say zero up to
  56. 1:32and including a hundred
  57. 1:33wait you mean you're going to write to
  58. 1:35this 100th element 101st
  59. 1:37element at 100 you don't have access to
  60. 1:39that you have access to 99.
  61. 1:41so the problem is that what if you had
  62. 1:43another array what if this is this were
  63. 1:44the beginning of another array this way
  64. 1:47well then you have access to that data
  65. 1:50it's some other malloc over here
  66. 1:51that's not an error to write to that
  67. 1:52from this program but you don't know
  68. 1:54that so here you know where you're
  69. 1:56setting to zero so this had key value
  70. 1:58you now write a zero to that
  71. 2:00and you don't find that error until much
  72. 2:02much later because
  73. 2:03only much later years later you actually
  74. 2:05visit that part and you say well i
  75. 2:06certainly
  76. 2:06that's never supposed to be zero now is
  77. 2:08zero because that that simple little
  78. 2:10loop
  79. 2:10overwrote it won't check it for you so
  80. 2:12watch out for your limits of your array
  81. 2:14zero up to and including
  82. 2:15uh less than so the error is obviously
  83. 2:17the equal sign here so
  84. 2:19make that a less than and that'll work
  85. 2:21okay so watch out watch your bounds
  86. 2:23this is a great example you cannot
  87. 2:26return
  88. 2:27a pointer into a memory stack space
  89. 2:31here's this little guy very simple
  90. 2:32here's this pointer function
  91. 2:34it sets a little cute little y it sets
  92. 2:36it in three little y
  93. 2:37sets into three and returns a pointer to
  94. 2:39it well you're not supposed to do that
  95. 2:41why well here is the pointer stack
  96. 2:43address and here's the content
  97. 2:45count is going to contain the three
  98. 2:46let's see hopefully okay
  99. 2:48so let's try it stack address equals
  100. 2:50pointer while this
  101. 2:51is running while that's running okay
  102. 2:54this is what the world looks like
  103. 2:57main has its stack frame and pointer has
  104. 2:59its stack frame everything's good and
  105. 3:01endpointer was y because y is a
  106. 3:02temporary variable and so that y
  107. 3:04lives right in there everybody's happy
  108. 3:06everything's good everything's there
  109. 3:08before the grasshoppers came back okay
  110. 3:10guys fans of a bug's life
  111. 3:12now when that returns at this equal sign
  112. 3:17this is what the world looks like
  113. 3:19there's no more that stack frame is gone
  114. 3:21it didn't erase it didn't scrub it but
  115. 3:23it's gone okay
  116. 3:24now really there was a there's a three
  117. 3:27in here
  118. 3:28still okay so stack address gets the
  119. 3:31address
  120. 3:32that i don't know what the address is
  121. 3:33who cares maybe this is at a thousand
  122. 3:35okay so it gets the thousand so stack
  123. 3:38here just gets a thousand so stack
  124. 3:39address points to that spot okay points
  125. 3:42to that spot
  126. 3:43so now i then say
  127. 3:46content equals to our stack address file
  128. 3:49if this is what's here remember i'm
  129. 3:50gonna keep this guy here that doesn't
  130. 3:51move that's never gonna move
  131. 3:53that points to that spot where there was
  132. 3:54a three there
  133. 3:56okay now content equals that follow the
  134. 3:59pointer and set it to content
  135. 4:01and print it out three that's great
  136. 4:05wait do it again go grab it again it
  137. 4:08prints garbage wait
  138. 4:09wha yeah you know why
  139. 4:12because during this printf that's what
  140. 4:15the world looked like
  141. 4:17printf is a function call functions have
  142. 4:20to make a stack frame
  143. 4:21who knows what internal material
  144. 4:23internal local variables
  145. 4:24what what's printf needs to do but it
  146. 4:27needs something
  147. 4:28so it clobbered over your three that you
  148. 4:31didn't have access to in the first place
  149. 4:32but it looked good for the first time it
  150. 4:34printed out three but this printf
  151. 4:36itself clobbered the three it grabbed it
  152. 4:39and then
  153. 4:40in the process of doing something it
  154. 4:41actually clobbered over it so it grabbed
  155. 4:44the three
  156. 4:44it's kind of funny well i mean this is
  157. 4:46the grab of the three that was the final
  158. 4:47three that was the factor three
  159. 4:49then printf clobbers it puts this number
  160. 4:51there so now
  161. 4:52that number it was a three now it says
  162. 4:55whatever this number is
  163. 4:56and the next time i go grab it i go
  164. 4:58follow that pointer i print out garbage
  165. 5:01that makes sense so i love this demo
  166. 5:04love this example of something that like
  167. 5:06how is it possible i just follow the
  168. 5:07pointer twice in succession and it's bad
  169. 5:09the second time
  170. 5:10yeah because printf collaborative
  171. 5:11because you weren't supposed to be there
  172. 5:12in the first place
  173. 5:14you're not supposed to use something
  174. 5:16after free here's a here's a his f which
  175. 5:18is a pointer to a struct foo
  176. 5:19i'm alex base for that i'm all set there
  177. 5:22and i free it that's great
  178. 5:24but then the line after it i'm gonna
  179. 5:26access the a
  180. 5:28element from that the a field from that
  181. 5:30you can't do that once you've freed f
  182. 5:31you have no access now you're pointing
  183. 5:33to this guy
  184. 5:33it's in there but who knows if it got
  185. 5:35clobbered there it may work
  186. 5:37but it may not work again you don't know
  187. 5:39what's happening in that malik space it
  188. 5:40may give you an error may give you a
  189. 5:41segmentation fault
  190. 5:42and may not check it at all all the
  191. 5:44things are are open and you don't want
  192. 5:45to have that happen so don't follow
  193. 5:47don't access once you've freed something
  194. 5:48you had no access to it anymore
  195. 5:50really important okay and again you
  196. 5:52might not find this out for
  197. 5:53for you know hours later and all of a
  198. 5:55sudden wait why does it crash well
  199. 5:57because you kind of corrupted something
  200. 5:58you're
  201. 5:58you're grabbed in there that's bad
  202. 6:02realic can move the data so that's
  203. 6:05another thing we talked about reality
  204. 6:06can reallocate a space so here's malik
  205. 6:08it makes 10 structures so here and there
  206. 6:11there's my f
  207. 6:12and f points to that there's my f points
  208. 6:14to that and look this says
  209. 6:16structure star g that's another pointer
  210. 6:18g which points to the same thing that's
  211. 6:20called sharing they both own the same
  212. 6:22thing
  213. 6:22but now this is a nice called realic
  214. 6:25okay
  215. 6:26in the process of reality it might just
  216. 6:28grow it here's 20 now it might just grow
  217. 6:30it if so then everything works then they
  218. 6:31both share in the spain
  219. 6:33f and g the space for that those those
  220. 6:35structures are still there
  221. 6:36except except what if really didn't have
  222. 6:39room there wasn't room here
  223. 6:41so i would have to have to move it i had
  224. 6:42to pick it up pick up the house and move
  225. 6:44it
  226. 6:45okay so now this is 20 long and now f
  227. 6:48is reassigned to there but what happens
  228. 6:51g is we call a stale pointer
  229. 6:54g points to the old guy and now you're
  230. 6:56going to access g
  231. 6:57bad bad could be bad could be fine could
  232. 7:00be bad you don't know again all these
  233. 7:02things are weird
  234. 7:03and don't do that don't free the wrong
  235. 7:05stuff
  236. 7:07here's a malloc for again the 10
  237. 7:09structures and now i'm going to say f
  238. 7:11plus plus
  239. 7:12so here's my malloc here guy here's my
  240. 7:15f points right to the front of it that's
  241. 7:17the zeroth element
  242. 7:18and now f plus plus f now points to
  243. 7:22the first element and now i free it
  244. 7:25add free has a list what is free take an
  245. 7:28address so free has an address that
  246. 7:30malik in the process of doing malik
  247. 7:32internally in their bookkeeping they
  248. 7:33keep track of all the malice they had
  249. 7:35which is like the pointer to the malik
  250. 7:36returned
  251. 7:37return return return return free gets
  252. 7:39won and it doesn't match
  253. 7:41i don't even know what free can do so
  254. 7:43don't do that because who knows what
  255. 7:44free would do
  256. 7:45if you could corrupted turtle storage
  257. 7:46you could erase things again it could
  258. 7:48leave it like it looks like it works but
  259. 7:49it actually doesn't work later all these
  260. 7:50things are really hard to track down
  261. 7:52don't do that don't double free either
  262. 7:55first line is a malic the second line is
  263. 7:58a free and then you free it again
  264. 8:00freeze only supposed to be done once
  265. 8:01never twice so that's the problem too
  266. 8:03you don't even know what's going to
  267. 8:04happen again you don't know what would
  268. 8:05happen as a result of that
  269. 8:07here's one way you lose the initial
  270. 8:08pointer again a memory leak i call void
  271. 8:11i've got this pointer that's a global
  272. 8:12guy out here plk
  273. 8:14i assign it which is great i can assign
  274. 8:17it uh
  275. 8:18to malik that's fine that's a total
  276. 8:20value that's that's valid
  277. 8:21but now i plk plus plus but i didn't
  278. 8:24know that outside of it so i now
  279. 8:26no longer outside have whatever p okay i
  280. 8:28can't say free plk anymore because i
  281. 8:30said again i moved it over
  282. 8:31i didn't know that outside i called you
  283. 8:33library your library move might be okay
  284. 8:35and all of a sudden my pok isn't
  285. 8:36pointing to the front anymore and i
  286. 8:37can't it's not the head anymore
  287. 8:38i can't do it always keep a head pointer
  288. 8:40to the front of that same idea don't do
  289. 8:42that
  290. 8:44it could be memory leak uh because again
  291. 8:46we don't know what's gonna happen to plk
  292. 8:47again boy i wish there were something i
  293. 8:51wish there was something to help me with
  294. 8:52this
  295. 8:52there is valgrind is a wonderful piece
  296. 8:55of software that will check for these
  297. 8:56things
  298. 8:57right when we check for memory leaks
  299. 8:58misuse of free writing of the end of
  300. 9:00arrays rackrun is really good about
  301. 9:01looking at your static code and deciding
  302. 9:03what's it going to do and what are some
  303. 9:04of the problems it's going to have
  304. 9:05really great it's essential for
  305. 9:07debugging c code please
  306. 9:09check that out i believe it's available
  307. 9:10in the labs in conclusion
  308. 9:13that's the end c has three memory
  309. 9:16management segments to which to allocate
  310. 9:17data
  311. 9:18static that's the global so don't worry
  312. 9:20about it stack again you don't worry
  313. 9:21about the operating system grows it and
  314. 9:22shrinks it automatically
  315. 9:23the heap again mostly don't worry about
  316. 9:25it except for making sure when you're
  317. 9:27malik you're always free exactly the guy
  318. 9:28you malek and don't forget not to do it
  319. 9:30and don't do it twice and don't move
  320. 9:31your pointer all those things you can do
  321. 9:32wrong
  322. 9:33don't do that and heap problems are the
  323. 9:36biggest source of problems in c
  324. 9:38so good luck with your c programming
  325. 9:40we'll see the next module
  326. 9:41take care

About this transcript

This page contains the full transcript of [CS61C FA20] Lecture 05.5 - C Memory Management: When Memory Goes Bad by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 2,098 words across 326 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.