Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called.

Similar presentations


Presentation on theme: "Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called."— Presentation transcript:

1 Chapter 12 Pointers and linked structures

2 2 Introduction  The data structures that expand or contract as required during the program execution is called dynamic data structures. These structures are especially useful for storing and processing data sets whose sizes change during the program execution.  Dynamic data structures are constructed using special variables called pointer variables (or simply pointers).  The main purpose of this chapter is to give an introduction to pointers and show how they can be used to implement a dynamic data structure known as a linked list.

3 3 Pointer variables  A variable is declared to be a pointer variable by including the pointer attribute in the attribute list of its declaration: type, attribute-list, pointer :: pointer-variable This declares that pointer-variable can be used to access a memory location where a value of the specified type and attributes can be stored. Example: Example: If the data values are strings, then a pointer to a memory location that can be used to store a string can be declared by character(8), pointer :: StringPtr Above pointer variable StringPtr may be used only to access memory locations in which character strings of length 8 can be stored.

4 4 Pointer variables  Similarly, type Inventory_Info integer :: Number real :: Price end type Inventory_Info type (Inventory_Info), pointer :: InvPtr declares that InvPtr is a pointer variable that may be used to access locations where structures of type Inventory_Info are stored.

5 5 The allocate statement  The allocate statement has the form allocate (list) or allocate (list, stat=status-variable)  The allocate statement was used to allocate memory for run-time arrays.  It is also used to acquire memory locations to associate with pointer variables during program execution. For example, the statement allocate (StringPtr) associates with StringPtr a memory location where a string such as “Computer” can be stored. We say that StringPtr “points” to this memory location, called a target, and we picture this with a diagram like the following: StringPtr -----------> Computer

6 6 The allocate statement  Each execution of an allocate statement acquires a new memory location and associates it with the specified pointer. Thus, if TempPtr is also a pointer declared by character (8), pointer :: TempPtr the statement allocate (TempPtr) acquires a new memory location pointed to by TempPtr: StringPtr ------------> Computer TempPtr -------------> ?

7 7 Pointer association  Pointer variables may be in one of three states: a) Undefined b) Associated c) Disassociated  Like all variables, each pointer variable is initially undefined. When a pointer points to a target, its status changes to associated: pointer ?  If this association is broken without associating the pointer variable with a new target, the pointer is said to be null or disassociated. A null pointer is commonly pictured using the ground symbol: pointer  Fortran provides the intrinsic function associated to test whether a pointer variable is associated with a target. A reference of the form associated (pointer) returns.true. If pointer is associated with a target and.false. otherwise. ?

8 8 Pointer association  The nullify statement can be used to change a pointer variable’s status to null. This statement has the form nullify (List-of-pointers)  When execution of a program begins, the program has a “pool” of available memory locations, called the free store. The effect of an allocate statement is to –1) Remove a block of memory from the free store. –2) Allocate that block to the executing program.  Since the size of the free store is limited, each allocate statement causes the pool of available memory to shrink. If more memory is needed than is available, program execution is halted unless a stat = clause is included in the allocate statement. In this case, the integer variable in the stat = clause will be assigned 0 if allocation is successful and a positive value otherwise.

9 9 Pointer association  Memory that is no longer needed can be returned to the free store by using a deallocate statement of the form deallocate (list) or deallocate (list, stat = status-variable) In the second form, the integer variable will be assigned 0 if deallocation is successful and a positive value otherwise, for example, if some of the pointer variables in the list are null.  Memory returned to the free store can be reallocated by subsequent allocate statements. The allocate and deallocate operations are thus complementary.

10 10 Pointer assignment  If pointer 1 and pointer 2 have the same type, an assignment statement of the form pointer 1 => pointer 2 causes pointer 1 to have the same association status as pointer 2, and if pointer 2 is associated, pointer 1 will point to the same target as pointer 2. The previous target (if any) pointed to by pointer 1 can no longer be accessed unless it is pointed to by some other pointer. The following diagrams illustrate: Before assignment: pointer 1 pointer 3 pointer 2 After assignment pointer 1 => pointer 2 : pointer 1 pointer 3 pointer 2

11 11 Pointer assignment Example: Example: Suppose that both StringPtr and TempPtr are pointer variables declared by character(8), pointer :: StringPtr, TempPtr and point to memory locations containing the strings Computer and Software, respectively: StringPtr TempPtr The assignment statement TempPtr => StringPtr causes TempPtr to point to the same memory location as StringPtr: Computer Software

12 12 Pointer assignment StringPtr TempPtr The string Software stored in the first location can no longer be accessed (unless it is pointed to by some other pointer).  A pointer assignment statement may also have the form pointer variable => target-variable where target -variable has the same type as pointer-variable but has the target attribute (and not the pointer attribute). The declaration of such a variable has the form type, attribute-list, target :: pointer-variable This assignment statement causes pointer-variable to point to target- variable, that is, to the memory location allocation to target-variable. Computer Software

13 13 Pointer assignment  To illustrate, consider the declarations: character (8), pointer :: StringPtr character (8), target :: Name = “John Doe” The statement StringPtr => Name causes StringPtr to point to the memory location allocated to Name ( at compiler time): Name StringPtr John Doe

14 14 Pointers in expressions  One important rule governs the use of pointers in expressions: When an associated pointer appears in an expression, it is automatically dereferenced; that is, the value stored in its target is used. If an associated pointer appears in an input list, an input value will be stored in its target. To illustrate, consider the declarations character (8), pointer :: StringPtr, NamePtr_1, NamePtr_2 character (8), target :: Name_1 = “Mary Doe”, Name_2 character (8) :: Product = “Computer” and suppose that a memory location has been associated with StringPtr by allocate (StringPtr) StringPtr The statements StringPtr = “Computer” and StringPtr = Product are ordinary assignment statements and store the string “Computer” in the memory location pointed to by StringPtr:

15 15 Pointers in expressions StringPtr  Dereferencing also occurs for pointer variables that point to target variables. For example, suppose that NamePtr1 and NamePtr2 point to the variables Name_1 and Name_2, respectively: NamePtr_1 => Name_1 NamePtr_2 => Name_2 The output statement print *, NamePtr_1 will then display the value stored in Name_1: Mary Doe The assignment statement NamePtr_2 = NamePtr_1 assigns the string Mary Doe stored in Name_1 to Name_2 and is therefore equivalent to the assignment statement Name_2 = Name_1 Computer

16 16 Pointers in expressions  As the previous example illustrates, there is a fundamental difference between the pointer assignment operator (=>) and the ordinary assignment operator (=). If both TempPtr and StringPtr are associated, StringPtr TempPtr the statement TempPtr = StringPtr copies the contents of the memory location pointed to by StringPtr into the location pointed to by TempPtr: StringPtr TempPtr Computer Software Computer

17 17 Pointers in expressions  This result is quite different from that produced by the pointer assignment TempPtr => StringPtr which causes TempPtr to point to the same memory location pointed to by StringPtr: StringPtr TempPtr Computer Software

18 18 Example for Pointers type, public :: point real :: x, y end type point type (point), target :: pt1 type (point), target :: pt pt => pt1 pt%x = 1.0 ! Equivalent to pt1%x = 1.0 pt%y = 2.0 ! Equivalent to pt1%y = 2.0.

19 19 Pointers and subprograms  Pointers (and targets) may be used as arguments of subprograms, satisfying the following conditions: 1) If a formal argument is a pointer variable, the corresponding actual argument must also be a pointer variable of the same type. 2) A pointer formal argument cannot have an intent attribute. 3) If a formal argument is a pointer (or target) variable, the subprogram must have an explicit interface.  When the subprogram is invoked, the association status of each actual argument that is a pointer is passed to the corresponding formal argument.  The value returned by a function may also be a pointer. In this case, the value must be returned by using a result clause to return the value assigned within the function to a local pointer variable.

20 20 Exercises module create_destroy. contains subroutine create() real, dimension (:), pointer :: p allocate (p(100))!Error checking omitted. !For clarity call calculate(p) end subroutine create subroutine calculate (x) real, pointer, dimension (:) :: x ! calculate using x. deallocate(x)!Error checking omitted. !For clarity end subroutine. end module create_destroy

21 21 Exercises  Study pages between 407-434 (Ellis’s Book)  Do self-test exercises 14.1 on page 431.

22 22 Implementing linked lists  Pointer variables are important because they are used to construct dynamic structures. In this section we will show how linked lists can be implemented using structures and pointers. LINKED LIST  A linked list consists of a collection of elements called nodes, each of which stores two items of information: (1) an element of the list (2) a link, which is a pointer, that indicates the location of the node containing the successor of this list element.  The nodes are represented in Fortran as structures having two kinds of components, data components and link components.  The data components have types that are appropriate for storing the necessary information, and the link components are pointers.

23 23 Implementing linked lists  For example  For example, the type of the nodes in the linked list Data Next Data Next Data Next NumList can be defined by type List_Node integer :: Data type (List_Node), pointer :: Next end type List_Node Each node in the list is a structure of type List_Node, consisting of two components. The first component Data is of integer type and is used to store the data. The second component Next is a pointer and points to the next node in the list. In addition to the nodes of the list in which to store the data items, a pointer to the first node is needed. Thus we declare a pointer variable NumList by type (List_Node), pointer :: NumList for the linked list of integers. 155017231996

24 24 Constructing a linked list  Suppose that the integers 1723 and 1996 have already been stored in a linked list: Data Next Data Next NumList and suppose that we wish to add 1550 to this list. In the construction, we use two pointers, NumList to point to the first node in the list and TempPtr as a temporary pointer: type (List_Node), pointer :: NumList, TempPtr we first acquire a new node temporarily pointed to by TempPtr, allocate (TempPtr) and store 1550 in the data component of this structure: TempPtr%Data = 1550 TempPtr NumList 17231996 1550 17231996

25 25 Constructing a linked list  This node can then be joined to the list by setting its link component so that it points to the first node: TempPtr%Next => NumList TempPtr NumList The pointer NumList is then updated to point to this new node: Numlist => TempPtr TempPtr NumList 1550 17231996 1550 17231996

26 26 Constructing a linked list  To construct the entire list, we could first initialize an empty list: nullify (NumList) and then repeat the preceding four statements three times, replacing 1550 by 1996 in the second assignment statement, then by 1723, and finally again using the value 1550. In practice, however, such linked lists are usually constructed by reading the data values rather than by assigning them with assignment statements. In this example, the linked list could be constructed by using the following program segment, where Item and AllocateStatus are integer variables.

27 27 Constructing a linked list ! Construction of a linked list ! Initially the list is empty nullify (NumList) ! Read the data values and construct the list do read *, Item if (Item == End_Data_Flag) exit allocate (TempPtr, stat = AllocateStatus) if (AllocateStatus /= 0) stop “*** Not enough memory ***” TempPtr%Data = Item TempPtr&Next => NumList NumList => TempPtr end do

28 28 Exercises  Study pages between 434-451 (Ellis’s Book).  Do self-test exercises 14.2 on page 449.


Download ppt "Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called."

Similar presentations


Ads by Google