[CS61C FA20] Lecture 05.3 - C Memory Management: Memory Locations — Transcript
Full transcript
- 0:00and welcome back is this fun as we're
- 0:03going through the series of lectures
- 0:04we're kind of revealing
- 0:05the onion revealing the abstraction
- 0:07layer and explaining how the really the
- 0:08system works so now i understand how to
- 0:10play with memory how to require how to
- 0:11request it either malik how to give it
- 0:13back and free
- 0:14really cool but there's some more
- 0:15details to this let's actually take
- 0:17take take a reflection of where do
- 0:20things really live
- 0:21really live this is really neat this is
- 0:24people have never seen this before this
- 0:25is
- 0:25you don't know this on your own if you
- 0:26just pick up c on your own you can be
- 0:28programmed say 100 years but you
- 0:29wouldn't know the things unless you
- 0:31get told this i always read the book and
- 0:33probably tell you but people don't read
- 0:34the book but if you just kind of pick it
- 0:35up on your own you're not going to do
- 0:37so don't forget global variables first
- 0:40of all so
- 0:41let's just summarize when you make a
- 0:43structured declaration
- 0:44that just declares a new type a
- 0:47structure that doesn't make any space
- 0:48for it that's just kind of like here's a
- 0:49new type i'm going to use
- 0:51but when you make a variable declaration
- 0:52that actually does reserve memory so
- 0:54that the first one doesn't the second
- 0:55one does reserve
- 0:57so so far we've talked about several
- 0:59ways to allocate memory for data you
- 1:01could have a local variable
- 1:02int i struck node list that's the node
- 1:05two fields value and next character star
- 1:08string
- 1:08that's the space for the pointer but not
- 1:10for the string itself int ar of n
- 1:12there's n integers got that or
- 1:15dynamically
- 1:16pointer recall struck node star malik
- 1:19sizeof struck node
- 1:20times n that's n nodes in a row there's
- 1:23an array of nodes rather than a linked
- 1:25list there's an array
- 1:26of those of those nodes that's pretty
- 1:27cool like the struct nodes i should say
- 1:30there's one more place so one way is a
- 1:32local variable the second is this maliki
- 1:34thing
- 1:35you don't even know where they live yet
- 1:36i'll tell you that a couple slides and
- 1:37then finally you could have a global
- 1:39variable
- 1:40if i say int global into my global here
- 1:43above main
- 1:44so main is your top level procedure
- 1:46always always in c
- 1:47but if i put michael above lane it's now
- 1:50in the global namespace you now have
- 1:51access to my global everywhere any
- 1:53subroutine including maine
- 1:55or sub or foo has access to read and
- 1:57write my global that's pretty cool so if
- 1:59you need to have
- 2:00you know some value that's going to be
- 2:01shared rather than passing it here
- 2:03just something that's there just out
- 2:04there now don't you don't overuse it you
- 2:06can obviously write the worst code in
- 2:07the world by having a billion globals
- 2:09but if you have some constant like a big
- 2:11table or something you can use that
- 2:12without having
- 2:13so gross code although probably it's
- 2:14better to pass things around but you can
- 2:16still
- 2:17you know small programs use global uh
- 2:19sparingly so use them sparingly
- 2:21and you'll be fine has global scope so
- 2:23now
- 2:24let's use some words use your words
- 2:27c has three pools of memory okay
- 2:30remember i had these local guys i had
- 2:31this mallet guy i had this global guy
- 2:33turns out that they live in three
- 2:35different places
- 2:37the first one static storage that's the
- 2:40global space it's static
- 2:42it static means it doesn't dynamic it's
- 2:44not dynamic it means it's frozen
- 2:46it does doesn't mean you can't change it
- 2:47but it means it can't change the size of
- 2:49it
- 2:50you can still change the values inside
- 2:51like my global can be written read and
- 2:53written to
- 2:54but that lives in the static so global
- 2:56variables live there
- 2:57it's permanent and there for the whole
- 2:59program so it's not going to grow
- 3:00okay so static doesn't move the stack is
- 3:03where your local values are you might
- 3:05have heard the stack
- 3:06stack overflow you've never heard how
- 3:08would it be let me find something on
- 3:09stack overflow now you know stack
- 3:11overflow we'll talk about why the stacks
- 3:12overflow
- 3:13but you got a stack local variable
- 3:15storage parameters
- 3:17return addresses all these things are in
- 3:20the stack
- 3:21and the heap is where your mallet goes
- 3:24so dynamic stuff
- 3:25is the heap i need to apologize in the
- 3:28basis of all computer scientists because
- 3:31the stack the name stack is the same as
- 3:3561bs a stack data structure
- 3:38and in fact they operate very much the
- 3:40same way
- 3:41okay so they're the same kind of the
- 3:43idea of the stack in memory
- 3:45and this what a stack is same thing
- 3:48the heap is not the same
- 3:52as the heap data structure so i
- 3:54apologize on basis of all computer
- 3:56scientists
- 3:56what went before me because it's a
- 3:59terrible
- 4:00name the heap is not what a heap is it's
- 4:02not stored it's not
- 4:03somehow represented using a heap it's
- 4:04not a heap is just a heap of memory okay
- 4:08don't think of the heap oh it must be
- 4:09stored like a heap structure it's not
- 4:11okay stack is stored like a hashtag
- 4:13operates like a stack
- 4:14the heap is not okay c
- 4:17really fluency requires you to know what
- 4:19those three things are
- 4:21this is why i think teaching c to cs1
- 4:24students to intro programmers
- 4:25is a terrible idea there's just too much
- 4:26to learn about how the machine works and
- 4:28how the code works how debug
- 4:29too much easier language python blocks
- 4:31based language something else
- 4:33but don't teach and see initially third
- 4:35third language is fine now you know how
- 4:36to program but i get to
- 4:37now now you learn how to program well so
- 4:40here's a picture
- 4:42the address space contains four regions
- 4:45this is pretty cool i love this picture
- 4:47fine line look like revealing the
- 4:49open the hood there's light shining out
- 4:50of this okay the stack
- 4:53local variables remember it starts at
- 4:55the top
- 4:56remember roughly you have access to
- 4:5832-bit machine
- 5:002 to the 32 0 to 2-3 minus 32-1
- 5:04bytes that you can read and write okay
- 5:05for now
- 5:07stack starts at the top and grows down
- 5:10poop
- 5:11so as you have more things as you
- 5:13increase the stack it's gonna grow down
- 5:15towards zero
- 5:18the heap grows up towards this middle
- 5:22space
- 5:24static data is locked in you know what
- 5:25the size of it is it's locked in and in
- 5:27fact the code
- 5:29when you run a program if i run foo i
- 5:31run some program i've run hello world
- 5:34the code for that gets loaded into
- 5:35memory as well so that
- 5:37memory footprint contains all the code
- 5:41all the static space you have all those
- 5:42globals
- 5:44any heap you've requested probably
- 5:45nothing initially and then
- 5:47whatever stack has initially probably
- 5:49has main information here like maybe the
- 5:51arguments passed in are appear
- 5:53okay why do we like this model
- 5:57well it's nice because if i had a
- 5:58program that had a lot of stack
- 6:00i had a lot of stackage but a little
- 6:01heap can you get the inquiry example
- 6:03like that
- 6:03what might it be well no heat means that
- 6:07i never call malik a lot of stack means
- 6:09maybe a really long recursive maybe a
- 6:11runaway recursion maybe a
- 6:13a big fractal or maybe i'm doing
- 6:14factorial of some huge number
- 6:16calls this cars cars is because those
- 6:18local variables all those function calls
- 6:20grow the stack
- 6:21okay we'll talk about this we're going
- 6:23to explain that a little bit more in a
- 6:24couple slides
- 6:26how about heap can you think of
- 6:28something where the heap grows up in
- 6:29almost no stack well no function calls
- 6:31so stack is going to grow really no
- 6:33arrays up there there are no
- 6:35no temporary variables that are used in
- 6:36main so the stack is pretty small and i
- 6:38have the first line is a massive call to
- 6:40malik
- 6:41now that he would grow up high do you
- 6:43know what program would allow you to
- 6:45have
- 6:45almost direct control of malik
- 6:49photoshop i want a new document i want
- 6:52it one million pixels by one million
- 6:53pixels
- 6:54that's no say maybe a thousand by a
- 6:55thousand that's a million pixels and now
- 6:57it has to grab it somewhere dynamically
- 6:59grabs it from the heap another
- 7:01difference between the heap and stack is
- 7:04you know ar square brackets that's nice
- 7:06except that you can't
- 7:07if i say ar of a million what if i don't
- 7:09have room for a million
- 7:10it'll crash but if i ask it through
- 7:13malik what'll malik return
- 7:15null so you can now have code that is
- 7:18resilient to memory failure if you use
- 7:20it malik
- 7:21versus using arrays we'll talk about
- 7:22more but that's kind of an example like
- 7:24a razor really fast i get it really fast
- 7:25malik might take a bit longer i'll get
- 7:27to this at a moment as well
- 7:28but malik has the ability to tell you if
- 7:31it can't get it for you whether you
- 7:32array if you want to make a million
- 7:34array maybe there's in room at all maybe
- 7:35you've heaped a lot and now there's no
- 7:36room here
- 7:37but it would crash if you say you know
- 7:39local variable
- 7:40open square bracket a million you can't
- 7:42get it crashed there's no way to catch
- 7:43it there's no way to save it
- 7:46codes at the bottom static data heap and
- 7:48the stack okay
- 7:49important and now but for now don't
- 7:51worry about how
- 7:53somebody prevents them from overloading
- 7:55over crossing each other okay so somehow
- 7:57that's prevented the os will handle the
- 7:58take 162
- 7:59to learn all about it so where are they
- 8:03allocated i've kind of said before if
- 8:04they're declared outside of procedure
- 8:06meaning outside of all procedures my
- 8:07global they're in the static area
- 8:09if they're local they're on the stack if
- 8:12they
- 8:12are in malik they're on the heap we said
- 8:14this before and by the way malik is a
- 8:16procedure
- 8:17now let's talk about what happens
- 8:19they're free with the procedure returns
- 8:20which means wait
- 8:21so foo calls bar and i get some local
- 8:23temporary space
- 8:24when bar goes away it's actually freed
- 8:26now all that stuff that bar had all
- 8:27those local variables a bar had
- 8:29all these arrays that bar had that are
- 8:30not mallet calls but arrays
- 8:32they go away when bar returns so that's
- 8:35actually kind of cool and maine is a
- 8:36procedure as well
- 8:38here's your stack let's go a little
- 8:39deeper on the stack so the stack frame
- 8:41includes
- 8:42the return address so how to go back to
- 8:45the previous guy
- 8:46important any parameters you have and
- 8:48space for any local variables
- 8:50so if you ever like foo calls bar calls
- 8:52baz you ever wonder how does it know
- 8:53where to come back to
- 8:54it's the stack that's how it does it the
- 8:57other key thing is the stack is
- 8:58continuous block of memory you'll see
- 9:00that mallet can actually be all over the
- 9:02place
- 9:03it can be uh here and there and there
- 9:05and that's going to be a problem but the
- 9:07stack is always continuous
- 9:08contiguous it grows like a stack unlike
- 9:11a stack it goes down normally a stack
- 9:12goes up and you think of it the stack is
- 9:14going down okay
- 9:15and the procedure ends the stack frame
- 9:17is tossed off
- 9:18so it frees it dynamically pretty cool
- 9:20so let's actually take a look at this
- 9:22let's make a little animation
- 9:24so i'm in maine okay got some space for
- 9:27maine some local stuff is going on in
- 9:28maine i'm happy
- 9:29i call af zero i gotta know where to
- 9:32come back to
- 9:32so i gotta i gotta somehow set up i have
- 9:35to somehow set up a structure where
- 9:37i need to know when a returns how to
- 9:40come back to me so i have to store
- 9:42the line after a0 the line right here
- 9:44this is the key thing
- 9:46right there that address that kind of in
- 9:50that address in the program needs to be
- 9:51stored somewhere so when a comes back
- 9:53i reload that's where i'm going to start
- 9:55from someone's got to remember that and
- 9:57it's the stack
- 9:58that's the key so whoops let me erase
- 10:01this here
- 10:03okay all right
- 10:06so now i call a
- 10:09that's the bottom of the stack pointer
- 10:10okay i'm going to now make a function
- 10:12call
- 10:13so i'm going to grow the stack
- 10:16i'm going to store my return address and
- 10:18any temporary any any i'm going to pass
- 10:20in any parameters here we go
- 10:21and i've now grown the stack and now
- 10:23that's where a
- 10:24is so now main and a are alive and the
- 10:27stack now has both of those active
- 10:29and so any temporary variables that are
- 10:30a are in that area and that's the blue
- 10:33try to color code them the same so you
- 10:34can kind of see the same
- 10:35okay well now a is going to call b
- 10:38so i better grow the stack move the
- 10:40stack pointer down
- 10:41okay by the way that's really fast okay
- 10:44to do this
- 10:45i basically take a stack pointer
- 10:46internally i'll tell you when we learned
- 10:47about assembly you'll learn about this i
- 10:49just move the stack printer down it's
- 10:50really fast okay to grow the stack is
- 10:52very very fast
- 10:54and to shrink the stack just move the
- 10:55stack pointer up pretty easy
- 10:57okay all this is now the room for b's
- 11:00local variables any parameters the n has
- 11:01got to be there somewhere all those
- 11:03things are there
- 11:03turns out that the n actually gets
- 11:05passed as a register too much detail now
- 11:07but
- 11:08some space for b to work with okay and
- 11:10to know how to get back
- 11:11to a then i go down to c
- 11:15same thing all we're doing is the same
- 11:16thing and i go down to d now here's the
- 11:18key
- 11:18i'm going to now invert this now d
- 11:20returns
- 11:21so watch what happens did i go back into
- 11:25the code
- 11:26and erase it grab my eraser and erase
- 11:28the member to make it all zeros
- 11:30no the answer is always no i don't have
- 11:32time no time no time it's almost like no
- 11:34time no time it's like
- 11:35my joke about ain't no free lunch no
- 11:38time no time i'm too busy
- 11:39i can't erase it so you're gonna notice
- 11:41interestingly that if d
- 11:43had some secret value secret value
- 11:45password
- 11:46up to the whole world's computer system
- 11:48equals
- 11:50bosco and you typed it in the local
- 11:52variable
- 11:53so it lived in here somewhere in here
- 11:56was bosco and now
- 11:59that was when the stack pointer was
- 12:01there and now i return from d
- 12:05and i go back well guess what like by
- 12:08like the powerpoint shows you it's still
- 12:10there even though i don't have access to
- 12:12it official can't be accessing anything
- 12:13past the stack
- 12:14bosco is still in memory interesting you
- 12:18should check that out it's really fun
- 12:20so now let me clear that up now c
- 12:22returns
- 12:24i just move the stack pointer up and
- 12:26immediately that space isn't
- 12:27zeroed out but is not space i can use
- 12:30anymore
- 12:31okay now b returns and a returns and i'm
- 12:33still in main doing my stuff
- 12:35isn't that cool so that's basically how
- 12:38that notice that was like a stack stack
- 12:39goes down
- 12:40stack goes up when you function called
- 12:41and you return uh
- 12:43d to c to b to a back into main now
- 12:46we're living here and now main is
- 12:47working with all those local variables
- 12:48doing all the right thing
- 12:49it's pretty cool and it knew how to get
- 12:51back because you had the stack the stack
- 12:52goes
- 12:53again you learn this all when you learn
- 12:54about assembly you need to know how to
- 12:56come back all that's stored in the stack
- 12:58all the local space is there and you
- 12:59free it by moving the stack down and
- 13:00moving stack up
- 13:01easy see the next lecture
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 05.3 - C Memory Management: Memory Locations by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 2,704 words across 434 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.