CNG242 Lecture3 Variables and Storage
CNG242 Lecture3 Variables and Storage
CNG242 Lecture3 Variables and Storage
1. Storage 4. Commands
• Array Variables • Assignment
2. Semantics of Assignment • Procedure Call
3. Variable Lifetime • Block commands
• Global Lifetime • Conditional commands
• Local Lifetime • Iterative statements
• Heap Variable Lifetime 5. Memory Representation
• Dangling Reference and Garbage 6. Summary
• Persistent Variable Lifetime
Introduction
Then, allocated/undefined.
Ready to use but value
unknown.
Then, storable
C example:
Dynamic Array Example
•Array size is defined when variable is allocated. Remains
constant afterwards.
Example: GNU Compiler Collection (GCC) supports many language features
not found in the ANSI/ISO standards for C and C++
•Perl example:
•C++ and object orient languages allow overload of [] operator to make flexible
arrays possible. STL (Standard Template Library) classes in C++ like vector,
map are like such flexible array implementations.
Copy semantics vs reference semantics
•Java follows copy semantics for primitive types. All other types (objects) are
reference semantics.
•Reference semantics cause problems from storage sharing (all operations effect
both variables). Deallocation of one makes the other invalid.
•Java provides copy semantic via a member function called copy(). Java
garbage collector avoids invalid values (in case of deallocation)
Example: Java reference semantics
• Declarations:
class Date {
int y, m, d;
public Date (int y, int m, int d) { … }
}
Date dateR = new Date(2004, 1, 1);
Date dateS = new Date(2004, 12, 25);
• Effect of reference semantics: dateR dateS
dateR dateS
dateS = dateR;
dateR.y = 2005; 2005
2004
2004 2004
11 12
11 25
Variable Lifetime
•Multiple instances of same local variable may alive at the same time in
recursive functions.
Variable Lifetime
• 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., stored in global variables).
• Ada is safer:
– After a heap variable is destroyed, pointers to it might
still exist.
– But pointers to local variables may not be stored in
global variables.
• Java is very safe:
– It has no deallocator.
– Pointers to local variables cannot be obtained.
Example: C dangling pointers
struct Date {int y, m, d;};
allocates a new
Date* dateP; Date* dateQ; heap variable
dateP = (Date*)malloc(sizeof Date);
dateP->y = 2004; dateP->m = 1; dateP->d
= 1;
dateQ = dateP; makes dateQ point
to the same heap
free(dateQ); variable as dateP
deallocates that heap
printf("%d4", dateP->y); variable (dateP and
dateP->y = 2005; dateQ are now
dangling pointers)
fails fails
Garbage Variables
•Garbage variables: The variables with lifetime still continue but there is
no way to access.
•When the pointer value is lost or lifetime of the pointer is over, heap
variable is unaccessible. (*p in examples)
Garbage Collection
•A solution to dangling reference and garbage problem:
•PL does management of heap variable deallocation automatically.
•This is called garbage collection. (Java, Lisp, ML, Haskell, most
functional languages) no call like free() or delete exists.
•Count of all possible references is kept for each heap variable.
•When reference count gets to 0 garbage collector deallocates the heap
variable.
• Garbage collector usually works in a separate thread when CPU is
idle.
•This way objects snapshot can be taken, saved, restored and object
continue from where it remains.
Commands
•Evaluates RHS expression and sets the value of the variable at RHS
• Example (Java):
float[] v1 = new float[4];
float[] v2 = new float[3];
v1 v2