Lifetime “The lifetime of a variable is the time during which the variable is bound to a specific memory location.” [p. 219] “…the lifetime of a variable.

Slides:



Advertisements
Similar presentations
Chapter 6 Data Types
Advertisements

Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Names and Bindings.
Chapter 5 Names, Bindings, and Scopes
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
CS 355 – Programming Languages
Chapter 5 Basic Semantics
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Chapter 10 Storage management
CS 330 Programming Languages 10 / 18 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
Slide 1 Chapter 5-b Attributes of variables. Slide 2 Attributes of variables { int sum = 5;... } sum int IdentifierType Value 0110.
Variables Six properties: Binding times of properties:
Run-Time Storage Organization
The Concept of Variables
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
Names and Bindings Introduction Names Variables The concept of binding Chapter 5-a.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Names, Bindings, and Scopes
Software II: Principles of Programming Languages Lecture 5 – Names, Bindings, and Scopes.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Chapter 5 Names, Bindings, and Scopes. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 5 Topics Introduction Names Variables The Concept.
ISBN Chapter 5 Names, Bindings, and Scopes.
Storage Bindings Allocation is the process by which the memory cell or collection of memory cells is assigned to a variable. These cells are taken from.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
Basic Semantics Associating meaning with language entities.
ISBN Chapter 5 Names, Bindings, and Scopes.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
ISBN Chapter 6 Data Types Pointer Types Reference Types Memory Management.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
ISBN Object-Oriented Programming Chapter Chapter
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Scope, and Bindings Programming Languages and Paradigms.
CHAPTER 4 VARIABLES & BINDING SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Data Types Chapter 6: Data Types Lectures # 11. Topics Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
Advanced Programming in C
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
Java Memory Management
Java Memory Management
Type Checking, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Dynamically Allocated Memory
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Binding Times Binding is an association between two things Examples:
Dynamic Memory.
Names, Bindings, and Scopes
Run-time environments
Presentation transcript:

Lifetime “The lifetime of a variable is the time during which the variable is bound to a specific memory location.” [p. 219] “…the lifetime of a variable begins when it is bound to a specific cell and ends when it is unbound from that cell.” [p. 219] Four categories –static –stack-dynamic –explicit heap-dynamic –implicit heap-dynamic

Memory organization AVAILABLE MEMORY STATIC code & static data HEAP dynamic data STACK local data: invocation records

static variables Bound to a memory location prior to execution. No run-time allocation needs to occur: efficient. Persistent: variable persists throughout execution of a program.

Example (C) #include “stdio.h”; int counter() { static int k = 0; return ++k; } int main() { printf(“Counter is %d \n”,counter()); return 0; } /* OUTPUT IS: Counter is 1 Counter is 2 */

Notes about example static variable k is allocated space in the static segment allocation happens once k’s lifetime is the that of the entire program k’s value persists from call to call

stack dynamic variables “storage bindings are created when their declaration statements are elaborated, but whose types are statically bound” [p. 220] Allocated on the run-time stack

Example (C) #include “stdio.h”; int counter() { int k = 0; return ++k; } int main() { printf(“Counter is %d \n”,counter()); return 0; } /* OUTPUT IS: Counter is 1 */

Notes about example stack-dynamic variable k is allocated space in the stack segment allocation happens each time function is called k’s lifetime is the that of the function invocation k’s value does not persist from call to call: it is reinitialized on each function execution

explicit heap-dynamic variables anonymous (nameless) variables created at runtime, allocated on the heap, and accessible only via indirection (a pointer) Example [p. 221] int *intnode; // Create a pointer... intnode = new int; // Create the heap-dynamic variable... delete intnode; // Deallocate the heap-dynamic variable // to which intnode points

Example (Java) public class Count private int k; public Count() { k = 0; } public int counter() { return ++k; } public static void main(String [] args) { Count c = new Count(); System.out.println(“Counter is ”+c.counter()); } /* OUTPUT IS: Counter is 1 Counter is 2 */

Notes about example instance variable k is allocated space in the heap segment allocation happens each time object is created is called k’s lifetime is the that of its object k’s value persists from call to call of the method many different independent k’s can co-exist

implicit heap-dynamic variables automatic heap-allocation JavaScript, [pg. 214]: list = [10.2, 3.5]; list = 47; Scheme –all allocation is done on heap –cons allocates a pair –environments are allocated on heap (so no runtime stack is needed for function calls).

Example (Scheme) (define count1 (lambda () (let ((k 0)) (set! k (+ k 1)) k ))) (define count2 (let ((k 0)) (lambda () (set! k (+ k 1)) k ))) (display (count1)) (newline) (display (count2)) (newline) /* OUTPUT IS: 1 2 */

Notes about example in count1 k is initialized each time function is called in count2 k is initialized when the function is defined

Scope “The scope of a variable is the range of statements in which the variable is visible. A variable is visible in a statement if it can be referenced in that statement.” Type basic approaches: –static scoping –dynamic scoping