Upload
paul-skinner
View
239
Download
1
Tags:
Embed Size (px)
Citation preview
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:
< variable-name , memory-address(or location) >
2- Memory ()- Each pair: <location , value>- Non-allocate locations are marked unused- Allocated location without value are marked undefined- Use overriding union to change this set
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)
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
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
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 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)
Evaluation• Change relation from tt’ to t|t’|’• Where and ’ are the starting and ending states
of the store