[CS61C FA20] Lecture 05.5 - C Memory Management: When Memory Goes Bad — Transcript
Full transcript
- 0:00and welcome back this last
- 0:03segment this last video on introduction
- 0:05to c
- 0:06the goal is to figure out how to teach
- 0:08you the things that you probably will do
- 0:10if we didn't tell you in advance
- 0:12these are the common mistakes people
- 0:13make when working with memory or
- 0:15dun dun dun when memory goes bad so this
- 0:18is just a review slide
- 0:19why do we do pointers at all well if i'm
- 0:21going to pass some value into a function
- 0:23i don't want to pass all the data it's
- 0:24via copy because it's called by value
- 0:26i want to pass a pointer so you can have
- 0:28access to my structure here my
- 0:30array or my big struct or something what
- 0:32are the drawbacks you've got to deal
- 0:33with these pointers you got to deal with
- 0:35the fact that when you make a malik and
- 0:36free request
- 0:37maybe you forget to do it maybe you're
- 0:39in the wrong order maybe you move the
- 0:40pointer around and you don't do it
- 0:41a lot of problems so dangly reference
- 0:43says i make a pointer i
- 0:44initialize a pointer then here is when
- 0:46i'm going to malik and set it to it but
- 0:48i start using it before
- 0:49i actually have set it to the right
- 0:51malik value
- 0:52there's another mini error of what
- 0:54happens when you start reading from this
- 0:55uninitialized array before you even
- 0:57write to it so that's an issue
- 0:59memory leak happens where you've
- 1:00reserved something from malik but
- 1:03then you never free it so a tardy freeze
- 1:05says you forget to free it
- 1:06you can also lose the pointer or you
- 1:08free it a little too late bad things can
- 1:09happen so
- 1:10free at the right time so this is the
- 1:12easiest case writing off the end of an
- 1:14array
- 1:14so here's foo it makes a hundred uh i'll
- 1:17i'll use a little picture here
- 1:19so it makes a little array i'll just
- 1:20write it that's my array and that pretty
- 1:22array
- 1:22and foo points to the beginning that's
- 1:24the zero guy there okay
- 1:26and at zero up to it's 100 elements so
- 1:28zero up to 99.
- 1:29well what does this loop say zero up to
- 1:32and including a hundred
- 1:33wait you mean you're going to write to
- 1:35this 100th element 101st
- 1:37element at 100 you don't have access to
- 1:39that you have access to 99.
- 1:41so the problem is that what if you had
- 1:43another array what if this is this were
- 1:44the beginning of another array this way
- 1:47well then you have access to that data
- 1:50it's some other malloc over here
- 1:51that's not an error to write to that
- 1:52from this program but you don't know
- 1:54that so here you know where you're
- 1:56setting to zero so this had key value
- 1:58you now write a zero to that
- 2:00and you don't find that error until much
- 2:02much later because
- 2:03only much later years later you actually
- 2:05visit that part and you say well i
- 2:06certainly
- 2:06that's never supposed to be zero now is
- 2:08zero because that that simple little
- 2:10loop
- 2:10overwrote it won't check it for you so
- 2:12watch out for your limits of your array
- 2:14zero up to and including
- 2:15uh less than so the error is obviously
- 2:17the equal sign here so
- 2:19make that a less than and that'll work
- 2:21okay so watch out watch your bounds
- 2:23this is a great example you cannot
- 2:26return
- 2:27a pointer into a memory stack space
- 2:31here's this little guy very simple
- 2:32here's this pointer function
- 2:34it sets a little cute little y it sets
- 2:36it in three little y
- 2:37sets into three and returns a pointer to
- 2:39it well you're not supposed to do that
- 2:41why well here is the pointer stack
- 2:43address and here's the content
- 2:45count is going to contain the three
- 2:46let's see hopefully okay
- 2:48so let's try it stack address equals
- 2:50pointer while this
- 2:51is running while that's running okay
- 2:54this is what the world looks like
- 2:57main has its stack frame and pointer has
- 2:59its stack frame everything's good and
- 3:01endpointer was y because y is a
- 3:02temporary variable and so that y
- 3:04lives right in there everybody's happy
- 3:06everything's good everything's there
- 3:08before the grasshoppers came back okay
- 3:10guys fans of a bug's life
- 3:12now when that returns at this equal sign
- 3:17this is what the world looks like
- 3:19there's no more that stack frame is gone
- 3:21it didn't erase it didn't scrub it but
- 3:23it's gone okay
- 3:24now really there was a there's a three
- 3:27in here
- 3:28still okay so stack address gets the
- 3:31address
- 3:32that i don't know what the address is
- 3:33who cares maybe this is at a thousand
- 3:35okay so it gets the thousand so stack
- 3:38here just gets a thousand so stack
- 3:39address points to that spot okay points
- 3:42to that spot
- 3:43so now i then say
- 3:46content equals to our stack address file
- 3:49if this is what's here remember i'm
- 3:50gonna keep this guy here that doesn't
- 3:51move that's never gonna move
- 3:53that points to that spot where there was
- 3:54a three there
- 3:56okay now content equals that follow the
- 3:59pointer and set it to content
- 4:01and print it out three that's great
- 4:05wait do it again go grab it again it
- 4:08prints garbage wait
- 4:09wha yeah you know why
- 4:12because during this printf that's what
- 4:15the world looked like
- 4:17printf is a function call functions have
- 4:20to make a stack frame
- 4:21who knows what internal material
- 4:23internal local variables
- 4:24what what's printf needs to do but it
- 4:27needs something
- 4:28so it clobbered over your three that you
- 4:31didn't have access to in the first place
- 4:32but it looked good for the first time it
- 4:34printed out three but this printf
- 4:36itself clobbered the three it grabbed it
- 4:39and then
- 4:40in the process of doing something it
- 4:41actually clobbered over it so it grabbed
- 4:44the three
- 4:44it's kind of funny well i mean this is
- 4:46the grab of the three that was the final
- 4:47three that was the factor three
- 4:49then printf clobbers it puts this number
- 4:51there so now
- 4:52that number it was a three now it says
- 4:55whatever this number is
- 4:56and the next time i go grab it i go
- 4:58follow that pointer i print out garbage
- 5:01that makes sense so i love this demo
- 5:04love this example of something that like
- 5:06how is it possible i just follow the
- 5:07pointer twice in succession and it's bad
- 5:09the second time
- 5:10yeah because printf collaborative
- 5:11because you weren't supposed to be there
- 5:12in the first place
- 5:14you're not supposed to use something
- 5:16after free here's a here's a his f which
- 5:18is a pointer to a struct foo
- 5:19i'm alex base for that i'm all set there
- 5:22and i free it that's great
- 5:24but then the line after it i'm gonna
- 5:26access the a
- 5:28element from that the a field from that
- 5:30you can't do that once you've freed f
- 5:31you have no access now you're pointing
- 5:33to this guy
- 5:33it's in there but who knows if it got
- 5:35clobbered there it may work
- 5:37but it may not work again you don't know
- 5:39what's happening in that malik space it
- 5:40may give you an error may give you a
- 5:41segmentation fault
- 5:42and may not check it at all all the
- 5:44things are are open and you don't want
- 5:45to have that happen so don't follow
- 5:47don't access once you've freed something
- 5:48you had no access to it anymore
- 5:50really important okay and again you
- 5:52might not find this out for
- 5:53for you know hours later and all of a
- 5:55sudden wait why does it crash well
- 5:57because you kind of corrupted something
- 5:58you're
- 5:58you're grabbed in there that's bad
- 6:02realic can move the data so that's
- 6:05another thing we talked about reality
- 6:06can reallocate a space so here's malik
- 6:08it makes 10 structures so here and there
- 6:11there's my f
- 6:12and f points to that there's my f points
- 6:14to that and look this says
- 6:16structure star g that's another pointer
- 6:18g which points to the same thing that's
- 6:20called sharing they both own the same
- 6:22thing
- 6:22but now this is a nice called realic
- 6:25okay
- 6:26in the process of reality it might just
- 6:28grow it here's 20 now it might just grow
- 6:30it if so then everything works then they
- 6:31both share in the spain
- 6:33f and g the space for that those those
- 6:35structures are still there
- 6:36except except what if really didn't have
- 6:39room there wasn't room here
- 6:41so i would have to have to move it i had
- 6:42to pick it up pick up the house and move
- 6:44it
- 6:45okay so now this is 20 long and now f
- 6:48is reassigned to there but what happens
- 6:51g is we call a stale pointer
- 6:54g points to the old guy and now you're
- 6:56going to access g
- 6:57bad bad could be bad could be fine could
- 7:00be bad you don't know again all these
- 7:02things are weird
- 7:03and don't do that don't free the wrong
- 7:05stuff
- 7:07here's a malloc for again the 10
- 7:09structures and now i'm going to say f
- 7:11plus plus
- 7:12so here's my malloc here guy here's my
- 7:15f points right to the front of it that's
- 7:17the zeroth element
- 7:18and now f plus plus f now points to
- 7:22the first element and now i free it
- 7:25add free has a list what is free take an
- 7:28address so free has an address that
- 7:30malik in the process of doing malik
- 7:32internally in their bookkeeping they
- 7:33keep track of all the malice they had
- 7:35which is like the pointer to the malik
- 7:36returned
- 7:37return return return return free gets
- 7:39won and it doesn't match
- 7:41i don't even know what free can do so
- 7:43don't do that because who knows what
- 7:44free would do
- 7:45if you could corrupted turtle storage
- 7:46you could erase things again it could
- 7:48leave it like it looks like it works but
- 7:49it actually doesn't work later all these
- 7:50things are really hard to track down
- 7:52don't do that don't double free either
- 7:55first line is a malic the second line is
- 7:58a free and then you free it again
- 8:00freeze only supposed to be done once
- 8:01never twice so that's the problem too
- 8:03you don't even know what's going to
- 8:04happen again you don't know what would
- 8:05happen as a result of that
- 8:07here's one way you lose the initial
- 8:08pointer again a memory leak i call void
- 8:11i've got this pointer that's a global
- 8:12guy out here plk
- 8:14i assign it which is great i can assign
- 8:17it uh
- 8:18to malik that's fine that's a total
- 8:20value that's that's valid
- 8:21but now i plk plus plus but i didn't
- 8:24know that outside of it so i now
- 8:26no longer outside have whatever p okay i
- 8:28can't say free plk anymore because i
- 8:30said again i moved it over
- 8:31i didn't know that outside i called you
- 8:33library your library move might be okay
- 8:35and all of a sudden my pok isn't
- 8:36pointing to the front anymore and i
- 8:37can't it's not the head anymore
- 8:38i can't do it always keep a head pointer
- 8:40to the front of that same idea don't do
- 8:42that
- 8:44it could be memory leak uh because again
- 8:46we don't know what's gonna happen to plk
- 8:47again boy i wish there were something i
- 8:51wish there was something to help me with
- 8:52this
- 8:52there is valgrind is a wonderful piece
- 8:55of software that will check for these
- 8:56things
- 8:57right when we check for memory leaks
- 8:58misuse of free writing of the end of
- 9:00arrays rackrun is really good about
- 9:01looking at your static code and deciding
- 9:03what's it going to do and what are some
- 9:04of the problems it's going to have
- 9:05really great it's essential for
- 9:07debugging c code please
- 9:09check that out i believe it's available
- 9:10in the labs in conclusion
- 9:13that's the end c has three memory
- 9:16management segments to which to allocate
- 9:17data
- 9:18static that's the global so don't worry
- 9:20about it stack again you don't worry
- 9:21about the operating system grows it and
- 9:22shrinks it automatically
- 9:23the heap again mostly don't worry about
- 9:25it except for making sure when you're
- 9:27malik you're always free exactly the guy
- 9:28you malek and don't forget not to do it
- 9:30and don't do it twice and don't move
- 9:31your pointer all those things you can do
- 9:32wrong
- 9:33don't do that and heap problems are the
- 9:36biggest source of problems in c
- 9:38so good luck with your c programming
- 9:40we'll see the next module
- 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.