13'POINTERS and DYNAMIC DATA STRUCTURES - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

13'POINTERS and DYNAMIC DATA STRUCTURES

Description:

one of the numerical methods (the least square fitting) Assignment: By using the method of least squares, fit a straight line through given ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 31
Provided by: nuzhet
Category:

less

Transcript and Presenter's Notes

Title: 13'POINTERS and DYNAMIC DATA STRUCTURES


1
13. POINTERS and DYNAMIC DATA STRUCTURES
  • Ellis Philips, 1998, p 407-453

2
  • It is often efficient to have pointer to a
    variable which can
  • be used to access the variable indirectly.
  • Pointers provide
  • 1) a more flexible alternative to allocatable
    array
  • 2) the ability to create and manipulate linked
    lists
  • (dynamic data structures)

3
Fundamental pointer concepts
  • Variables
  • - Scalars (They contain data.)
  • - Arrays (They contain data.)
  • - Pointers (They do not contain data instead they
    point to
  • a scalar or array variable where the data is
    actually stored.)
  • They are used in situations where data entities
    are being
  • created and destroyed dynamically while a program
    is
  • executing.
  • We can save the computer time and memory by using
  • pointers.

4
  • real, pointer p
  • p is a pointer that can point to objects of type
    real.
  • type(employee), pointer q
  • q is a pointer that can point to objects of the
    derived type
  • employee.

5
  • The general pattern for a pointer type
    declaration statement
  • is
  • type specifier, attribute list, pointer list
    of pointer
  • variables
  • The type specifier specifies what type of objects
    can be
  • pointed to,
  • the attribute list gives the other variables of
    the data type,
  • and
  • the list of pointer variables is a list of all
    points being defined.

6
Target attribute
  • real a
  • real, target b
  • real, pointer p
  • integer, pointer q
  • The variable p can point to the variable b
    because the types
  • match and b has the target attribute it can not
    point to a
  • because a does not have the target attribute. The
    variable q
  • can not point b because it is of the wrong type.

7
Association
  • A pointer can be associated with a target by a
    pointer
  • assignment statement.
  • pointer gt target
  • where pointer is a variable with the pointer
    attribute and
  • target is a variable which has either the target
    attribute
  • or the pointer attribute and which has the same
    type,
  • type parameters and rank as the pointer variable.

8
  • Example of pointer assignment
  • integer, pointer p,q,r
  • integer, target a,b
  • pgta ! p points to a
  • qgta ! q also points to a
  • rgtb ! r points to b
  • pgtb ! Now q points to a
  • ! p and r point to b
  • Example of pointer assignment where the target is
    a pointer
  • real, pointer u,v,w
  • real, target x
  • ugtx ! u points to x
  • vgtu ! v points to x
  • ugtw ! u now has an undefined association
    status.

9
  • Nullify statement breaks the association between
    the specified pointers
  • and their targets, setting the pointer
    association status of each pointer to
  • disassociated.
  • nullify (list of pointers)
  • real, target a,b
  • real, pointer p,q
  • pgta ! p points to a
  • qgta ! q also points to a
  • nullify (p) ! p is disassociated and q still
    points to a
  • pgtb ! p now points to b
  • nullify (p,q) ! p and q are disassociated

10
  • Because of the importance in many applications
    using
  • pointers, F includes an intrinsic function
    associated that will
  • return the association status of a pointer. This
    function can be
  • used in two ways with one argument or with two.
  • associated (p)
  • p is a variable with the pointer attribute. The
    reference function
  • has the logical value true if the pointer is
    currently associated
  • with a target and false if it is not.
  • If the reference to this function contains the
    second argument,
  • that argument must have the target attribute and
    the result of
  • the function reference will be true if and only
    if the pointer is
  • associated with the specified target.

11
  • It is strongly recommended that pointers should
    always be
  • either associated with a target variable
    immediately after
  • their declaration or nullified thereby ensuring
    that their
  • status is disassociated.
  • real, pointer a,b,c
  • integer, pointer p,q,r
  • nullify (a,b,c,p,q,r)

12
  • Restrictions on the use of the pointer and target
    attributes
  • A variable with the parameter attribute can not
    have either the pointer or the target attribute.
  • 2) A variable must not be given both the target
    attribute and the pointer attribute.

13
Using pointers in expressions
  • An example of an arithmetic expression involving
    a pointer
  • integer, pointer p, q
  • integer, target i1, j2
  • pgti ! p points to i
  • qgtj ! q points to j
  • pq1 ! assignment (to i)
  • If (p-1 q) then ! equality test and pointer
    assignment
  • pgtj
  • end if
  • pq1 ! assignment (to j)

14
Improving the efficiency of programs involving
large objects
  • type(large) large_1, large_2,temp
  • templarge_1
  • large_1large_2
  • large_2temp
  • This involves three copies of large amounts of
    data and
  • also involves the extra storage space for the
    variable temp.

15
  • Solution to this problem
  • type (large), target large_1, large_2
  • type (large), pointer p1, p2
  • p1gtlarge_1 ! p1 points to large_1
  • p2gtlarge_2 ! p2 points to large_2
  • ! Now work with p1 and p2 instead of large_1 and
    large_2
  • ! Interchange pointers so that p1 points to
    large_2 and
  • ! p2 points to large_1
  • p1gt large_2 ! p1 points to large_2
  • p2gt large_1 ! p2 points to large_1
  • No large objects are copied instead only two
    pointers are
  • reset.

16
  • type, public point
  • real x,y
  • end type point
  • type(point), target pt1
  • type(point), pointer pt
  • ptgtpt1
  • ptx1.0 !Equivalent to pt1x1.0
  • pty2.0 ! Equivalent to pt1y2.0
  • read , pt
  • The read statement will expect to read two real
    numbers
  • which will be read into the two components pt1x
    and
  • pt1y.

17
Pointers and arrays
  • The target of a pointer can also be an array
  • real, dimension(), pointer p_array
  • character(len5), dimension(,,), pointer
    p_array2
  • The array pointers p_array and p_array2 may be
    associated
  • with any arrays having matching type, type
    parameters and
  • rank and which have the target attribute. The
    extents and
  • index bounds of the arrays can be of any
    magnitude.

18
  • An example of the use of array pointers
  • integer n,u,v,w
  • ! Assign values to n,u,v,w
  • real, dimension(10), target a
  • real, dimension(n), target b
  • character(len5), dimension(u,v,w), target d
  • character(len5), dimension(v,10,20), target e
  • character(len4), dimension(v,10,20), target f
  • real, dimension(), pointer p
  • character(len5), dimension(,,), pointer q
  • pgta ! Associate p with array a
  • pgtb ! Associate p with array b
  • qgtd ! Associate q with array d
  • qgte ! Associate q with array e
  • Note that q would not be allowed to point to the
    array f because its length
  • attribute does not match.

19
  • One of the most powerful aspects of array
    pointers is their
  • use as a means of dynamically creating space for
    an array
  • when required, and releasing it when it is no
    longer required.
  • allocate (pointer(dimension specification))
  • or
  • allocate(pointer(dimension specification),statst
    atus)
  • Pointer is a pointer array, dimension
    specification is the
  • specification of the extents for each dimension,
    status is an
  • integer variable which will be assigned the value
    zero. If the
  • allocation is successful and a processor
    dependent positive
  • value if there is an error, for example if there
    is not enough
  • memory available.

20
  • A pointer allocate statement can be released by
    means of
  • a deallocate statement.
  • deallocate(pointer)
  • deallocate(pointer, statstatus)
  • See the program in page 421
  • The general rule is that a pointer deallocate
    statement must
  • not be used to deallocate any object, scalar or
    array that
  • was not allocated by a pointer allocate
    statement. Only
  • objects dynamically created by a pointer allocate
    statement
  • can be destroyed by a pointer deallocate
    statement.

21
Pointers as components of derived types
  • A pointer can also be a component of a derived
    type.
  • Such a pointer component of a derived type can
    point to
  • an object of any intrinsic type or to any
    accessible derived
  • type including the type being defined.

22
  • Example
  • type, public mine
  • integer i
  • real, dimension(), pointer p
  • end type mine
  • type(mine) a,b
  • allocate (ap(10),bp(20))
  • ai1
  • ap0.0 ! Fill all elements of ap with 0
  • bi2
  • bp(1192)0.0 ! Fill odd-numbered elements of
    bp with 0
  • bp(2202)1.0 ! Fill even-numbered elements of
    bpwith 1

23
Pointers as arguments to procedures
  • The allocatable arrays cannot be used as dummy
    arguments
  • of procedures. On the other hand, pointers (and
    targets) are
  • allowed to be procedure dummy arguments but only
    as long
  • as the following conditions are met.
  • If a dummy argument is a pointer, then the actual
    argument must be a pointer with the same type,
  • type parameters and rank.
  • 2) A pointer dummy argument cannot have the
    intent attribute.

24
  • module create_destroy
  • .
  • contains
  • subroutine create ()
  • real, dimension(), pointer p
  • allocate (p(100))
  • .
  • call calculate (p)
  • end subroutine create
  • subroutine calculate(x)
  • real, pointer, dimension() x
  • ! calculate using x
  • deallocate(x)
  • .
  • end subroutine calculate
  • .
  • end module create_destroy
  • By deallocation of x, the actual argument p is
    deallocated in subroutine

25
Pointer-valued functions
  • Using a pointer as an argument to a procedure
  • module small
  • public even_pointer
  • contains
  • function even_pointer(a) result (p)
  • real, dimension(), target, intent(in) a
  • real, dimension(), pointer p
  • pgta(22) ! p points to an array section
  • end function even_pointer
  • end module small
  • The result of even_pointer is an array pointer to
    the even
  • numbered elements of the input array a.

26
Linked lists and other dynamic data structures
  • One of the most common uses of pointers to create
    linked
  • lists.
  • A linked List
  • Head
  • ?
  • Data Fields Tail
  • Pointer ? Data Fields ?
  • Pointer ? Data Fields Pointer
  • The first item in the list is referred to as the
    head of the list while the last
  • item is called the tail.

27
  • A binary tree
  • Root
  • ?
  • Data fields
  • Pointer ? Data fields
  • Data fields ? Pointer Pointer ?
  • Pointer? ? Pointer ?
  • ? Pointer ? ?
  • ?

28
  • The single node from which the tree grows is
    referred as
  • the root of the tree. While each of the linked
    lists which
  • make up the complete tree is referred to as a
    branch.
  • A tree which splits into two branches at each
    node is called
  • a binary tree one which splits into three at each
    node is
  • called a a ternary tree and so on.
  • Trees are very useful ways of representing many
    natural
  • and artificial structures. Linked lists and
    binary trees
  • provide power data structuring capabilities
    especially
  • when used in recursive algorithms.
  • See Example 14.3

29
  • In-class problem session 11
  • Objective Using all steps of programming in F
    language and learning
  • one of the numerical methods (the least square
    fitting)
  • Assignment
  • By using the method of least squares, fit a
    straight line through given
  • data points below and calculate b0 and b1
    constants of the fitted line
  • (use subroutine, array and open statements).
  • X Y
  • 0 124
  • 5 78
  • 10 54
  • 15 35
  • 20 30
  • 25 21
  • 30 22
  • 35 18

30
Data fitting by least squares approximation
Write a Comment
User Comments (0)
About PowerShow.com