+ All Categories
Home > Documents > Synthesis, Analysis, and Verification Lecture 13 Dynamic Allocation.

Synthesis, Analysis, and Verification Lecture 13 Dynamic Allocation.

Date post: 30-Mar-2015
Category:
Upload: meaghan-peet
View: 214 times
Download: 1 times
Share this document with a friend
Popular Tags:
12
Synthesis, Analysis, and Verification Lecture 13 Dynamic Allocation
Transcript
Page 1: Synthesis, Analysis, and Verification Lecture 13 Dynamic Allocation.

Synthesis, Analysis, and VerificationLecture 13

Dynamic Allocation

Page 2: Synthesis, Analysis, and Verification Lecture 13 Dynamic Allocation.

class List { List next; }public static void main(){ //alloc={}, next=x.null List first = new List(); //alloc={o1}, next=x.null List second = new List();//alloc={o1,o2}, next=x.null first.next = second; //alloc={o1,o2}, next=x.if(x==o1) o2 else null second.next = first; //alloc={o1,o2}, next=x.if(x==o2) o1

else if (x==o1) o2 else null

}

null

AllObjects \ alloc

o1 o2 o3 ...next

next

next

Linked List Example

Page 3: Synthesis, Analysis, and Verification Lecture 13 Dynamic Allocation.

class List { List next; }public static void main(){ //alloc={}, next=x.null List first = new List(); //alloc={o1}, next=x.null List second = new List();//alloc={o1,o2}, next=x.null first.next = second; //alloc={o1,o2}, next=x.if(x==o1) o2 else null second.next = first; //alloc={o1,o2}, next=x.if(x==o2) o1

else if (x==o1) o2 else null

}

o1

first

o2

second

nextnull

next

AllObjects \ alloc

o3 ...

next

Linked List Example

Page 4: Synthesis, Analysis, and Verification Lecture 13 Dynamic Allocation.

Linked List Exampleclass List { List next; }public static void main(){ //alloc={}, next=x.null List first = new List(); //alloc={o1}, next=x.null List second = new List();//alloc={o1,o2}, next=x.null first.next = second; //alloc={o1,o2}, next=x.if(x==o1) o2 else null second.next = first; //alloc={o1,o2}, next=x.if(x==o2) o1

else if (x==o1) o2 else null

}

o1

first

o2

second

next

null

next

AllObjects \ alloc

o3 ...

next

Page 5: Synthesis, Analysis, and Verification Lecture 13 Dynamic Allocation.

Memory Allocation in Java

x = new C();y = new C();assert(x != y); // fresh object references-distinct

Why should this assertion hold? How to give meaning to ‘new’ so we can prove it?

Page 6: Synthesis, Analysis, and Verification Lecture 13 Dynamic Allocation.

A View of the World

Everything exists, and will always exist.(It is just waiting for its time to become allocated.)It will never die (but may become unreachable).alloc : Obj Boolean i.e. alloc : Set[Obj]x = new C(); ^defult constructor

Page 7: Synthesis, Analysis, and Verification Lecture 13 Dynamic Allocation.

New Objects Point Nowhere

class C { int f; C next; C prev; }this should work:

x = new C(); assert(x.f==0 && x.next==null && x.prev==null)

x = new C();

Page 8: Synthesis, Analysis, and Verification Lecture 13 Dynamic Allocation.

If you are new, you are known by few

class C { int f; C next; C prev; }Assume C is the only class in the programLonely object: no other object points to it.Newly allocated objects are lonely! x = new C();

Page 9: Synthesis, Analysis, and Verification Lecture 13 Dynamic Allocation.

Remember our Model of Java Arrays

length : Array -> intdata : Array -> (Int -> Int) or simply: Array x Int -> Int

assert assert

data= data( (a,i):= x)

class Array { int length; data : int[]}a[i] = x

y = a[i]

Page 10: Synthesis, Analysis, and Verification Lecture 13 Dynamic Allocation.

Allocating New Array of Objectsclass oArray { int length; data : Object[]}x = new oArray[100]

Page 11: Synthesis, Analysis, and Verification Lecture 13 Dynamic Allocation.

D-Linked List

assume P; if (first == null) { first = n; n.next = null; n.prev = null; } else { n.next = first; first.prev = n; n.prev = null; first = n; } assert Q;

Page 12: Synthesis, Analysis, and Verification Lecture 13 Dynamic Allocation.

How to prove such verification conditions automatically?


Recommended