Arrays LAST TODAY NEXT Integers Array mechanics Searching arrays - - PowerPoint PPT Presentation
Arrays LAST TODAY NEXT Integers Array mechanics Searching arrays - - PowerPoint PPT Presentation
Arrays LAST TODAY NEXT Integers Array mechanics Searching arrays Memory model Correctness Aliasing Safety Dataypes so far Basic (a.k.a. small types) : int, string, bool, char Integers represent numbers (rendered using 2s
LAST Integers TODAY Array mechanics Memory model Aliasing Safety NEXT Searching arrays Correctness
Dataypes so far
- Basic (a.k.a. small types) : int, string, bool, char
- Integers
- represent numbers (rendered using 2’s complement)
- represent bit patterns
- Safety constraints on operations
- x / y, x % y
- x << k, x >> k
Learning resources
C0 Memory Model
Local Memory Contains values of local variables May only contain values of small types
int square(int x){ return x * x; }
x
C0 Memory Model
Local Memory Contains values of local variables May only contain values of small types
int main(){ int x = 100; int y = square(x); return y; }
x y
Function calls
Local Memory Contains values of local variables May only contain values of small types
int square(int x) { return x * x; } int main(){ int x = 10; int y = square(x); return y; }
main
10
x y square
10
x
100
A new dataype
- int, string, bool, char
- t[] array whose elements are of type t
small not small
C0 Memory Model
Local Memory Allocated Memory Contains values of local variables May only contain values of small types We can have addresses to the memory cells and store those addresses in local memory
C0 array facts
alloc_array(t, n)
- Initializes to a default value of type t
- Precondition: n >= 0
- Postcondition: length of allocated array is n
A[i]
- Indexable by integers [0, n)
- Precondition: 0 <= i < n
Arrays in C0 examples with coin
Arrays in C0
- -> int[] A = alloc_array(int, 5);
A is 0x13500000 (int[] with 5 elements)
memory address
Local Memory X
0xB8
Allocated Memory
5 6 7
0xB8
memory address
int[] X = alloc_array(int, 3); X[0] = 5; X[1] = 6; X[2] = 7;
int[] X = alloc_array(int, 3); X[0] = 5; X[1] = 6; X[2] = 7; int[] Y = X;
Local Memory X
0xB8
Allocated Memory
5 6 7
0xB8
Y
0xB8
int[] X = alloc_array(int, 3); X[0] = 5; X[1] = 6; X[2] = 7; int[] Y = X; Y[2] = 9;
Local Memory X
0xB8
Allocated Memory
5 6 9
0xB8
Y
0xB8
Local Memory X
0xB8
Allocated Memory
5 6 9
0xB8
Y
0xB8
true
int[] X = alloc_array(int, 3); X[0] = 5; X[1] = 6; X[2] = 7; int[] Y = X; Y[2] = 9; X == Y;
Local Memory X
0xB8
Allocated Memory
5 6 9
0xB8
Y
0xB8 0xC4
Z
5 6 9
0xC4
false
int[] X = alloc_array(int, 3); X[0] = 5; X[1] = 6; X[2] = 7; int[] Y = X; Y[2] = 9; int[] Z = alloc_array(int, 3); Z[0] = 5; Z[1] = 6; Z[2] = 9; X == Z;
int[] X = alloc_array(int, 3); X[0] = 5; X[1] = 6; X[2] = 7; int[] Y = X; int[] Z = alloc_array(int, 3); X = alloc_array(int, 3); Y = alloc_array(int, 3);
- ld X still accessible via Y
- ld X will be garbage collected
Contracts for array
- perations
- alloc_array(t, n)
- //@requires
- //@ensures
- A[i] (both read and write)
- //@requires
- \length(A)
- //@ensures
0 <= i && i < \length(A) \length(result) == n n >= 0 \result >= 0
Example: copying an array
- Write function array_copy, which will copy contents of
a given array to a new array and return the new array
- What should the parameters be?
- Pre and post conditions
- Code example using array_copy:
int[] X = alloc_array(int, 3); int[] Y = array_copy(X,…);
Y should be a new array containing the same elements as in X!
First attempt
int[] array_copy(int[] A) { return A; }
Y ends up being an alias for X, not what we want!
int[] X = alloc_array(int, 3); int[] Y = array_copy(X);
client(caller,user) code
Second attempt
There is no primitive for getting length of a C0 array. \length(A) is allowed only in contracts.
int[] array_copy(int[] A) { int[] B = alloc_array(int,???); … }
Second attempt
int[] array_copy(int[] A, int n) { int[] B = alloc_array(int,n); … } int[] X = alloc_array(int, 3); int[] Y = array_copy(X,5);
client(caller,user) code
safe? correct?
should not be allowed
Precondition added
int[] array_copy(int[] A, int n) //@requires n == \length(A); { int[] B = alloc_array(int,n); … return B; } int[] X = alloc_array(int, 3); int[] Y = array_copy(X,3);
client code
Precondition added
B is an alias to A, after array_copy returns, B is garbage collected.
int[] array_copy(int[] A, int n) //@requires n == \length(A); { int[] B = alloc_array(int,n); B = A; return B; } int[] X = alloc_array(int, 3); int[] Y = array_copy(X,3);
client code
Third attempt
int[] array_copy(int[] A, int n) //@requires n == \length(A); { int[] B = alloc_array(int,n); for (int i = 0; i < n; i++) { B[i] = A[i]; } return B; }
Safety on the function side
- Is A[i] safe?
- Is B[i] safe?
need to show 0 <= i < length of A/B
}
Using precondition, loop guard, and postcondition of alloc_array
int[] array_copy(int[] A, int n) //@requires n == \length(A); { int[] B = alloc_array(int,n); for (int i = 0; i < n; i++) { B[i] = A[i]; } return B; }
Safety on the function side
int[] array_copy(int[] A, int n) //@requires n == \length(A); { int[] B = alloc_array(int,n); for (int i = 0; i < n; i++) //@loop_invariant 0 <= i; { B[i] = A[i]; } return B; }
show that it is a valid loop invariant
Proof
INIT: obvious PRES: assume 0<= i, and show 0 <= i’
int[] array_copy(int[] A, int n) //@requires n == \length(A); { int[] B = alloc_array(int,n); for (int i = 0; i < n; i++) //@loop_invariant 0 <= i; { B[i] = A[i]; } return B; }
i’ = i + 1 i + 1 >= 0 ( i ! = int_max() because i < n by the loop guard)
Safety on the caller side
- Is int[] J = array_copy(I,3) safe?
- Is int[] K = array_copy(J,3) safe?
int main() { int[] I = alloc_array(int, 3); for (int i=0; i<3; i++) { I[i] = i+5; // I is [5, 6, 7] } int[] J = array_copy(I,3); int[] K = array_copy(J,3); return 0; }
int[] array_copy(int[] A, int n) //@requires n == \length(A);
What is length of J?
Postcondition added
int[] array_copy(int[] A, int n) //@requires n == \length(A); //@ensures \length(\result) == n; { int[] B = alloc_array(int,n); for (int i = 0; i < n; i++) //@loop_invariant 0 <= i; { B[i] = A[i]; } return B; }
Effects visible to the client?
visible to caller int[] array_copy(int[] A, int n) //@requires n == \length(A); //@ensures \length(\result) == n; { int[] B = alloc_array(int,n); for (int i = 0; i < n; i++) //@loop_invariant 0 <= i; { B[i] = A[i]; } if (n > 0) A[0] = 42; return B; }
not visible to caller int[] array_copy(int[] A, int n) //@requires n == \length(A); //@ensures \length(\result) == n; { int[] B = alloc_array(int,n); for (int i = 0; i < n; i++) //@loop_invariant 0 <= i; { B[i] = A[i]; } A = alloc_array(int, 10); if (n > 0) A[0] = 42; return B; }