Advanced Access Types 1 www.drew-hamilton.com When we use access types • Flexibility • Recursion • Applications such as spreadsheets, operating systems, compilers, databases. • Polymorphism: using an array of access types each pointing to objects of different types. • Classwide programming: pointers to objects of classwide types. (tagged records) • Many others...Advanced Access Types 2 www.drew-hamilton.com Why we use access types • To allow different objects or subprograms to play a particular role at various times, by having certain access-type variables point to the objects or subprograms currently playing that role. • To allow the same data to be shared by several parts of a data structure, so that only one copy needs to be kept up to date and only one copy takes up space. • To allow pointers to large data structures to be copied rather than the data structures themselves. • To construct variables that refer to arrays of different sizes at different times. • To invoke subprograms associated with given positions in a table.Advanced Access Types 3 www.drew-hamilton.com Categories of Access-to-Object Types Access-to- constant types Access-to-variable types Pool-specific access types General access types = Access-to-variable types + Access-to-constant typesAdvanced Access Types 4 www.drew-hamilton.com Operations of Access Values Initialization p1: nodepointer_type; --initialized to null. Allocation p1 := new node; --address, value undefined Assignment p2:= p1; --copies access value from p1 to p2 Dereferencing S:= p1.all -- copies the value designated by p1 into S. Equality/Inequality What is the difference between the following statements? If P1 = P2 then…. If P1.all = P2.all then….Advanced Access Types 5 www.drew-hamilton.com Nomenclature Three types of Access-to-object types: Pool-specific access types type poolptr_type is access integer; General access-to-variable types type varptr_type is access all integer; General access-to-constant types type contptr_type is access constant integer; Two types of General access types: General access-to-variable types General access-to-constant typesAdvanced Access Types 6 www.drew-hamilton.com Pool-specific access types P1, P2, P3: ptr_type; Storage Pool P3 P2 P1Advanced Access Types 7 www.drew-hamilton.com Pool-specific access types Storage Pool P3 P2 P1 P1:=new record_type; …... Storage has now been dynamically allocated. P1 points to the address of the allocated storage in the memory poolAdvanced Access Types 8 www.drew-hamilton.com Pool-specific access types Storage Pool P3 P2 P1 …... P3:= P1; P3 now points to the same storage address as P1.Advanced Access Types 9 www.drew-hamilton.com Pool-specific access types Storage Pool P3 P2 P1 …... P1:=new record_type; …...Advanced Access Types 10 www.drew-hamilton.com Pool-specific access types Storage Pool P3 P2 P1 …... P1:=new record_type; --another invocation …... …... New storage has been allocated for P1. Storage is now allocated which can no longer be accessed. Note: at this point we have no knowledge about the contents of any of these records.Advanced Access Types 11 www.drew-hamilton.com Birth of an Access Type 1. Type declaration: type pointer_type is access <all><constant> datatype; 2. Variable declaration: p: pointer_type; at this point, p is initialized to null at elaboration time. 3. p:= new integer; dynamically allocated, address is no longer null; value pointed to unknown.Advanced Access Types 12 www.drew-hamilton.com Verify this for yourself now • Modify program ATO2 by adding a boolean flag to determine if Ipool_ptr is null initially at execution time. • Then determine if Ipool_ptr is still null after the first dynamic allocation of storage, prior to assigning a value to the integer Ipool_ptr points to.Advanced Access Types 13 www.drew-hamilton.com Access types in memory Activation Records Heap Global Memory Free Space Dynamic Allocation: • All pool-specific access types use DA • General access types may use DA Storage allocated in heap at run-time. Declared: • All general access types may be assigned to an aliased object at compile-time. ORAdvanced Access Types 14 www.drew-hamilton.com PE: Difference Between Addresses and Values • Edit the program file Output_Address.adb • Implement one each of the three access to object types set access integers. • Declare/allocate variables as needed. • Return the both the storage address and the value pointed to and display them.Advanced Access Types 15 www.drew-hamilton.com Deallocation • Why? Once storage to an access type is dynamically allocated, that memory is unavailable for reuse. • Unchecked Deallocation allows storage allocated by an allocator to return storage to the pool. LRM 13.11. • Access-to-constant types may not be deallocated. Therefore, access-to-constant types should be allocated unless they can remain in the heap throughout the execution of the program. • Some implementations support programmer defined allocation and deallocation of storage pools. Chapter 19.5Advanced Access Types 16 www.drew-hamilton.com Access-type Lifetime Rule An attribute X’Access yielding a result belonging to an access type T is only allowed if X can remain in existence at least as long as T. • Look at Illegal_program_2 on page 357 of Cohen. • When procedure Set_Pointer ceases to exist, so does the local access type, hence scope rules cannot be circumvented by the assignment.Advanced Access Types 17 www.drew-hamilton.com Next Lesson • Project 1 Due. • Access type
View Full Document