Semantics of Arrays and Pointers By: M. Reza Heydarian Introduction Pointers Arrays Semantics of Arrays Semantics of Pointers
Introduction (Memory Management Overview) -Three Categories of Memory 1- Static Memory For values whose storage requirement:. Are known before runtime. Remain constant during running program 2- Run-time Stack - For dispatching active methods: Their local variables and parameter-argument linkages - Structure of stack is predictable and regular 3- Heap
Heap -Least structured of the three memory categories -Contain all other values that are dynamically allocated during run-time -Structure is far less predictable and regular -Becomes fragmented -Active memory blocks are not contiguous -Heap blocks are accessed indirectly using pointers (pointers are located in stack or heap)
Figure of Memory -addresses {0,…,n} called the program’s address space -h and n are initially defined at the beginning of run-time
Using two sets to manage Memory 1- Environment ( m ): - m is a method.- m is set of pairs: 2- Memory ( ) -Each pair: -Non-allocate locations are marked unused -Allocated location without value are marked undefined -Use overriding union to change this set
An Example of Sets
Pointers -Memory address (reference) -Indirection in referencing a value or a collection of value -Bane of reliable software development -Programmer must restore heap blocks -Some language(e.g.Java) remove explicit pointers
Arrays -Ordered sequences of values with same type -May have any number of dimensions -Its memory is dynamically allocated -Share the same scope rules as simple variables -In different language may be:.allocated space in either static, stack, or heap area.different indexing (e.g. C,C++,Java all use 0-origin indexing).different way to locate element in memory (e.g. row major order)
A semantic view of Memory in definition of Array
Semantic of arrays -By Jay semantic function.one dimensional array.heap allocation strategies.are accessed by a pointer in the stack -Using two functions:.new: find a contiguous unused block.delete
new and delete functions
allocate function
deallocate function
Semantic of expression evaluation
Denotational semantics of Array Hoare’s view: Array values are functions (Assume values of elements are Nat. Type)
Denotational semantics of Array(cont’) A new semantic function: Some semantic equations: - Array Variables
Denotational semantics of Array(cont’) - Array Application - Array Assignment - Array Declaration
Semantics of Pointers -Basic operations on references (Pointers):. Allocation r = ref 5; |> r : Ref Nat. Dereferencing !r;|> 5 : Nat. Assignment r := 7;|> unit : Unit
Pointers and Aliasing r = s = r => s = 13
Pointers and Compound Types We can create references to any type. Including function. e.g. Array of numbers (use references to function). e.g. Type is: Ref (Nat Nat)
Operational Semantics Typing Evaluation Store Typing
Typing
Evaluation Change relation from t t’ to t| t’| ’ Where and ’ are the starting and ending states of the store
Set of values and terms
Rules
Rules (cont’)
Store Typing
References 1- Programming Languages: Principles and Paradigms Allen Tucker and Robert Noonan. 2- Types and Programming Languages Benjamin C. Pierce 3- Theory of Programming Languages John C. Reynolds