Quick viewing(Text Mode)

Of Memory Global Space the Call Stack Heap Space Or

Of Memory Global Space the Call Stack Heap Space Or

9/29/19

The Three “Areas” of Memory Global Space

The • This is the area you “start with” Heap § First memory area you learned to visualize Global § A place to store “global variables” Space § Lasts until you quit Python p id2 • What are global variables? § Any assignment not in a function definition Call Stack § Also modules & functions! § Will see more on this in a bit

The Call Stack Heap Space or “The Heap”

• The area where call frames live • Where the “folders” live § Call frames are created on a function call § Stores only folders • Can only access indirectly id2 § May be several frames (functions call functions) Point3 § Must have a variable with identifier § Each frame deleted as the call completes § Can be in global space, call stack x 0.0 • Area of volatile, temporary memory • MUST have variable with id y 0.0 incr_x 2 § If no variable has id, it is forgotten § Less permanent than global space z 0.0 § Think of as “scratch” space q id2 § Disappears in Tutor immediately § But not necessarily in practice • Primary focus of Assignment 2 § Role of the garbage collector

Modules and Global Space Functions and Global Space

• Importing a module: import math • A function definition… def to_centigrade(x): Body § Creates a global variable Global Space § Creates a global variable return 5*(x-32)/9.0 (same name as module) math id5 (same name as function) Heap Space Global Space § Creates a folder for body § Puts contents in a folder id5 to_centigrade id6 § Puts folder id in variable • Module variables module • Variable vs. Call Heap Space • Module functions pi 3.141592 >>> to_centigrade id6 § Puts folder id in variable e 2.718281 function • from keyword dumps >>> to_centigrade (32) functions Body contents to global space 0.0

1 9/29/19

Recall: Call Frames Function Access to Global Space

1. Draw a frame for the call Call: to_centigrade(50.0) • All function definitions Global Space a 4 (for globals.py) 2. Assign the argument value to_centigrade 1 are in some module to the parameter (in frame) • Call can access global change_a 3. Execute the function body x 50.0 space for that module § Look for variables in the frame a 3.5 § If not there, look for global § math.cos: global for math variables with that name § temperature.to_centigrade # globals.py 4. Erase the frame for the call What is happening here? uses global for temperature """Show how globals work""" def to_centigrade(x): • But cannot change values a = 4 # global space 1 return 5*(x-32)/9.0 § Makes a new ! def change_a(): § Why we limit to constants a = 3.5 # local variable

Frames and Helper Functions Frames and Helper Functions Not done. Do not erase! 1. def last_name_first(s): Call: last_name_first('Walker White'): 1. def last_name_first(s): Call: last_name_first('Walker White'): 2. """Precond: s in the form last_name_first 4 2. """Precond: s in the form last_name_first 5 3. 'first-name last-name' """ 3. 'first-name last-name' """ s 'Walker White' s 'Walker White' 4. first = first_name(s) 4. first = first_name(s) 5. last = last_name(s) 5. last = last_name(s) first 'Walker' 6. return last + ',' + first 6. return last + ',' + first 7. first_name 10 . . . last_name 15

8. def first_name(s): s 'Walker White' 13. def last_name(s): s 'Walker White' 9. """Precond: see above""" 14. """Precond: see above""" 10. end = s.find(' ') 15. end = s.rfind(' ') 11. return s[0:end] 16. return s[end+1:]

The Call Stack Anglicize Example

• Functions are stacked Global § Cannot remove one above Frame 1 Space w/o removing one below calls Frame 2 § Sometimes draw bottom up calls (better fits the metaphor) Frame 3 calls Call Stack • Stack represents memory Frame 4 as a high water mark calls § Must have enough to keep the Frame 65 entire stack in memory § Error if cannot hold stack

2