{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

12 - Memory Locations For Variables Click to edit Master...

Info icon This preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Click to edit Master subtitle style Chapter Twelve Modern Programming Languages, 2nd ed. Memory Locations For Variables 11
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter Twelve Modern Programming Languages, 2nd ed. A Binding Question Variables are bound (dynamically) to values Those values must be stored somewhere Therefore, variables must somehow be bound to memory locations How? 22
Image of page 2
Chapter Twelve Modern Programming Languages, 2nd ed. Functional Meets Imperative Imperative languages expose the concept of memory locations: a := 0 Store a zero in a ’s memory location Functional languages hide it: val a = 0 Bind a to the value zero But both need to connect variables to values represented in memory So both face the same binding question 33
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter Twelve Modern Programming Languages, 2nd ed. Outline Activation records Static allocation of activation records Stacks of activation records Handling nested function definitions Functions as parameters Long-lived activation records 44
Image of page 4
Chapter Twelve Modern Programming Languages, 2nd ed. Function Activations The lifetime of one execution of a function, from call to corresponding return, is called an activation of the function When each activation has its own binding of a variable to a memory locations, it is an activation-specific variable (Also called dynamic or automatic ) 55
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter Twelve Modern Programming Languages, 2nd ed. Activation-Specific Variables In most modern languages, activation- specific variables are the most common kind: 66 fun days2ms days = let val hours = days * 24.0 val minutes = hours * 60.0 val seconds = minutes * 60.0 in seconds * 1000.0 end;
Image of page 6
Chapter Twelve Modern Programming Languages, 2nd ed. Block Activations For block constructs that contain code, we can speak of an activation of the block The lifetime of one execution of the block A variable might be specific to an activation of a particular block within a function: 77 fun fact n = if (n=0) then 1 else let val b = fact (n-1) in n*b end;
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter Twelve Modern Programming Languages, 2nd ed. Other Lifetimes For Variables Most imperative languages have a way to declare a variable that is bound to a single memory location for the entire runtime Obvious binding solution: static allocation (classically, the loader allocates these) 88 int count = 0; int nextcount() { count = count + 1; return count; }
Image of page 8
Chapter Twelve Modern Programming Languages, 2nd ed. Scope And Lifetime Differ In most modern languages, variables with local scope have activation-specific lifetimes , at least by default However, these two aspects can be separated, as in C: 99 int nextcount() { static int count = 0; count = count + 1; return count; }
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter Twelve Modern Programming Languages, 2nd ed. Other Lifetimes For Variables Object-oriented languages use variables whose lifetimes are associated with object lifetimes Some languages have variables whose values are persistent: they last across multiple executions of the program Today, we will focus on activation-specific variables 1010
Image of page 10
Chapter Twelve
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern