9-1 9 Variables and lifetime  Variables and storage  Simple vs composite variables  Lifetime: global, local, heap variables  Pointers  Commands 

Slides:



Advertisements
Similar presentations
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Advertisements

Dynamic memory allocation
Chapter 6 Data Types
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
Names and Bindings.
Chapter 7: User-Defined Functions II
Run-time organization  Data representation  Storage organization: –stack –heap –garbage collection Programming Languages 3 © 2012 David A Watt,
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Kernighan/Ritchie: Kelley/Pohl:
Informática II Prof. Dr. Gustavo Patiño MJ
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Run time vs. Compile time
The Concept of Variables
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Imperative Programming
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Fundamentals of C and C++ Programming Control Structures and Functions.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
3-1 © 2004, D.A. Watt, University of Glasgow 3 Variables and Storage  A simple storage model.  Simple and composite variables.  Copy semantics vs reference.
Bindings and scope  Bindings and environments  Scope and block structure  Declarations Programming Languages 3 © 2012 David A Watt, University.
Introduction to Data Structures Systems Programming.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Compiler Construction
CNIT 133 Interactive Web Pags – JavaScript and AJAX Advanced topic - variable.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Basic Semantics Associating meaning with language entities.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Programming Languages and Paradigms Imperative Programming.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Introduction to Data Structures Systems Programming Concepts.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
5.13 Recursion Recursive functions Functions that call themselves
Type Checking, and Scopes
Programming Languages and Paradigms
CNIT 133 Interactive Web Pags – JavaScript and AJAX
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Programming Paradigms
Java Programming: Guided Learning with Early Objects
Memory Allocation CS 217.
CSC215 Lecture Control Flow.
Programming Languages and Paradigms
Presentation transcript:

9-1 9 Variables and lifetime  Variables and storage  Simple vs composite variables  Lifetime: global, local, heap variables  Pointers  Commands  Expressions with side effects Programming Languages 3 © 2012 David A Watt, University of Glasgow

9-2 What are variables?  In functional PLs (and in mathematics), a variable stands for a fixed (but possibly unknown) value.  In imperative and OO PLs, a variable contains a value. The variable may be inspected and updated as often as desired. –Such a variable can be used to model a real-world object whose state changes over time.

9-3 Storage  To understand such variables, assume an abstract storage model: –A store is a collection of cells, each of which has a unique address. –Each cell is either allocated or unallocated. –Each allocated cell contains either a simple value or undefined. –An allocated cell can be inspected, unless it contains undefined. –An allocated cell can be updated at any time. true ‘X’ ? undefined unallocated cells allocated cells

9-4 Simple vs composite variables  A simple variable is one that contains a primitive value or pointer. –A simple variable occupies a single allocated cell.  A composite variable is one that contains a composite value. –A composite variable occupies a group of adjacent allocated cells.

9-5 Composite variables  A variable of a composite type has the same structure as a value of the same type. For instance: –A tuple variable is a tuple of component variables. –An array variable is a mapping from an index range to a group of component variables.  Depending on the PL, a composite variable can be: –totally updated (all at once), and/or –selectively updated (one component at a time).

9-6 Example: C composite variables  Declaration and updating of struct variables: struct Date {int y, m, d;} struct Date xmas, today; xmas.d = 25; xmas.m = 12; xmas.y = 2008; today = xmas; selective updating total updating  Declaration and updating of array variable: float a[10]; a[i] = ; selective updating

9-7 Lifetimes  Every variable is created at some definite time, and destroyed at some later time when it is no longer needed.  A variable’s lifetime is the interval between its creation and destruction.  A variable occupies cells only during its lifetime. When the variable is destroyed, these cells may be deallocated. –And these cells may subsequently be re-allocated to other variable(s).

9-8 Global vs local vs heap variables  A global variable’s lifetime is the program’s entire run-time. It is created by a global declaration.  A local variable’s lifetime is an activation of a block. It is created by a declaration within that block, and destroyed on exit from that block.  A heap variable’s lifetime is arbitrary, but bounded by the program’s run-time. It can be created at any time (by an allocator), and may be destroyed at any later time. It is accessed through a pointer.

9-9 Example: C global and local variables (1)  Outline of C program: extern int x1, x2; void main () { int m1; float m2; … f(); … e(); … } void f () { float f1; int f2; … e(); … } void e () { int e1; … } local variable local variables global variables

9-10 Example: C global and local variables (2)  Lifetimes of global and local variables:  Global and local variables’ lifetimes are nested. They can never be overlapped. nested overlapped lifetime of e1 lifetime of m1, m2 lifetime of e1 lifetime of f1, f2 lifetime of x1, x2 e() f() e() main()

9-11 Example: local variables and recursion (1)  Outline of C program: void main () { float m; … r(3); … } void r (int n) { int i; if (n > 1) { … r(n-1); … } }

9-12 Example: local variables and recursion (2)  Lifetimes of global and local variables: lifetime of i lifetime of m r(3) r(2) main() r(1)  Note: A local variable of a recursive procedure/function has several nested lifetimes.

9-13 Example: C heap variables (1)  Outline of C program: struct IntNode {int elem; IntList succ;} typedef struct IntNode * IntList; IntList c (int h, IntList t) { // Return an IntList with head h and tail t. IntList ns = (IntList) malloc (sizeof IntNode); ns->elem = h; ns->succ = t; return ns; } void d (IntList ns) { ns->succ = ns->succ->succ; }

9-14 Example: C heap variables (2)  Outline (continued): void main { IntList l1, l2; l1 = c(3, c(5, c(7, NULL))); l2 = c(2, l1); d(l1); }

9-15 Example: C heap variables (3)  After initializing l1 and l2 :  After calling d(l1) : unreachable l2 l heap variables l2 l1

9-16 Example: C heap variables (4)  Lifetimes of local and heap variables:  Heap variables’ lifetimes can overlap one another and local/global variables’ lifetimes. lifetime of 7-node lifetime of l1, l2 lifetime of 5-node lifetime of 3-node lifetime of 2-node c(7, …) main() c(5, …) c(3, …) c(2, …) d(…)

9-17 Allocators and deallocators  An allocator is an operation that creates a heap variable, yielding a pointer to that heap variable. E.g.: –C’s allocator is a library function, malloc(). –Java’s allocator is an expression of the form “ new C ( … ) ”.  A deallocator is an operation that explicitly destroys a designated heap variable. E.g.: –C’s deallocator is a library function, free(). –Java has no deallocator at all.

9-18 Reachability  A heap variable remains reachable as long as it can be accessed by following pointers from a global or local variable.  A heap variable’s lifetime extends from its creation until: –it is destroyed by a deallocator, or –it becomes unreachable, or –the program terminates.

9-19 Pointers (1)  A pointer is a reference to a particular variable. (In fact, pointers are sometimes called references.)  A pointer’s referent is the variable to which it refers.  A null pointer is a special pointer value that has no referent.  A pointer is essentially the address of its referent in the store. –However, each pointer also has a type, and the type of a pointer allows us to infer the type of its referent.

9-20 Pointers (2)  Pointers and heap variables can be used to represent recursive values such as lists and trees.  But the pointer itself is a low-level concept. Manipulation of pointers is notoriously error- prone and hard to understand.  For example, the C assignment “ p->succ = q; ” appears to manipulate a list, but which list? Also: –Does it delete nodes from the list? –Does it stitch together parts of two different lists? –Does it introduce a cycle?

9-21 Dangling pointers (1)  A dangling pointer is a pointer to a variable that has been destroyed.  Dangling pointers arise when: –a pointer to a heap variable still exists after the heap variable is destroyed by a deallocator –a pointer to a local variable still exists at exit from the block in which the local variable was declared.  A deallocator immediately destroys a heap variable; all existing pointers to that heap variable then become dangling pointers. –Thus deallocators are inherently unsafe.

9-22 Dangling pointers (2)  C is highly unsafe: –After a heap variable is destroyed, pointers to it might still exist. –At exit from a block, pointers to its local variables might still exist (e.g., if stored in global variables).  Java is very safe: –It has no deallocator. –Pointers to local variables cannot be obtained.

9-23 Example: C dangling pointers  Consider this C code: struct Date {int y, m, d;} typedef Date * DatePtr; DatePtr date1 = (DatePtr) malloc(sizeof Date); date1->y = 2008; date1->m = 1; date1->d = 1; DatePtr date2 = date1; free(date2); printf("%d4", date1->y); date2->y = 2009; deallocates that heap variable – date1 and date2 now contain dangling pointers makes date1 point to a newly-allocated heap variable makes date2 point to that same heap variable behaves unpredictably

9-24 Commands (1)  A command (often called a statement) is a program construct that will be executed to update variables.  Commands are characteristic of imperative and OO PLs (but not functional PLs).  Simple commands: –A skip command is a command that does nothing. –An assignment command is a command that uses a value to update a variable. –A procedure call is a command that calls a proper procedure with argument(s). Its net effect is to update some variables.

9-25 Commands (2)  Compound commands: –A sequential command is a command that executes its sub-commands in sequence. –A conditional command is a command that chooses one of its sub-commands to execute. –An iterative command is a command that executes its sub-command repeatedly. This may be: definite iteration (where the number of repetitions is known in advance) indefinite iteration (where the number of repetitions is not known in advance). –A block command is a command that contains declarations of local variables, etc.

9-26 Example: Java assignment commands  Java single assignment: m = n + 1;  Java multiple assignment: m = n = 0;  Java assignment combined with binary operator: m += 7; n /= b; equivalent to “ m = m+7; ” equivalent to “ n = n/b; ”

9-27 Example: Java conditional commands  Java if-command: if (x > y) out.print(x); else out.print(y);  Java switch-command: Date today = …; switch (today.m) { case 1: out.print("JAN"); break; case 2: out.print("FEB"); break;  case 11: out.print("NOV"); break; case 12: out.print("DEC"); } Breaks are essential here!

9-28 Example: Java iterative commands (1)  Java while-command: Date[] dates; … int i = 0; while (i < dates.length) { out.print(dates[i]); i++; }  Java for-commands (both forms): for (int i = 0; i < dates.length; i++) out.print(dates[i]); for (Date d : dates) out.print(d);

9-29 Example: Java iterative commands (2)  Java do-while-command: static String render (int n) { String s = ""; int m = n; do { char d = (char)(m % 10) + '0'; s = d + s; m /= 10; } while (m > 0); return s; }  Here the loop condition is evaluated after each repetition of the loop body.

9-30 Example: Java block command  Java block command: if (x > y) { int z = x; x = y; y = z; }

9-31 Expressions with side effects  The primary purpose of evaluating an expression is to yield a value.  In most imperative and OO PLs, evaluating an expression can also update variables – these are side effects.  In C and Java, the body of a function is a command. If that command updates global or heap variables, calling the function has side effects.  In C and Java, assignments are in fact expressions with side effects: “V = E” stores the value of E in V as well as yielding that value.

9-32 Example: side effects  The C function getchar(fp) reads a character and updates the file variable that fp points to.  The following C code is correct: char ch; while ((ch = getchar(fp)) != NULL) putchar(ch);  The following C code is incorrect (why?): enum Gender {FEMALE, MALE}; Gender g; if (getchar(fp) == 'F') g = FEMALE; else if (getchar(fp) == 'M') g = MALE; else 