Fortran 简明教程

Fortran - Pointers

在大多数编程语言中,指针变量存储对象的内存地址。然而,在 Fortran 中,指针是一个数据对象,不仅可以存储内存地址,还具有更多功能。它还包含一个特定对象的一些其他信息,比如类型、级别、扩展名和内存地址。

In most programming languages, a pointer variable stores the memory address of an object. However, in Fortran, a pointer is a data object that has more functionalities than just storing the memory address. It contains more information about a particular object, like type, rank, extents, and memory address.

通过分配或指针分配,可以将指针与目标关联。

A pointer is associated with a target by allocation or pointer assignment.

Declaring a Pointer Variable

使用 pointer 属性声明指针变量。

A pointer variable is declared with the pointer attribute.

以下示例展示了指针变量的声明 −

The following examples shows declaration of pointer variables −

integer, pointer :: p1 ! pointer to integer
real, pointer, dimension (:) :: pra ! pointer to 1-dim real array
real, pointer, dimension (:,:) :: pra2 ! pointer to 2-dim real array

指针可以指向 −

A pointer can point to −

  1. An area of dynamically allocated memory.

  2. A data object of the same type as the pointer, with the target attribute.

Allocating Space for a Pointer

allocate 语句允许为指针对象分配空间。例如 −

The allocate statement allows you to allocate space for a pointer object. For example −

program pointerExample
implicit none

   integer, pointer :: p1
   allocate(p1)

   p1 = 1
   Print *, p1

   p1 = p1 + 4
   Print *, p1

end program pointerExample

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

1
5

当不再需要时,应使用 deallocate 语句清空已分配的存储空间,并避免累积未使用且不可用的内存空间。

You should empty the allocated storage space by the deallocate statement when it is no longer required and avoid accumulation of unused and unusable memory space.

Targets and Association

目标是另一个普通变量,为其预留了空间。必须使用 target 特性声明目标变量。

A target is another normal variable, with space set aside for it. A target variable must be declared with the target attribute.

使用关联运算符 (⇒) 将指针变量与目标变量关联起来。

You associate a pointer variable with a target variable using the association operator (⇒).

让我们重写前一个示例来说明这个概念 −

Let us rewrite the previous example, to demonstrate the concept −

program pointerExample
implicit none

   integer, pointer :: p1
   integer, target :: t1

   p1=>t1
   p1 = 1

   Print *, p1
   Print *, t1

   p1 = p1 + 4

   Print *, p1
   Print *, t1

   t1 = 8

   Print *, p1
   Print *, t1

end program pointerExample

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

1
1
5
5
8
8

空指针可以是 −

A pointer can be −

  1. Undefined

  2. Associated

  3. Disassociated

在上面的程序中,我们使用 ⇒ 运算符将指针 p1 与目标 t1 关联起来了。关联的函数测试指针的关联状态。

In the above program, we have associated the pointer p1, with the target t1, using the ⇒ operator. The function associated, tests a pointer’s association status.

nullify 语句将指针与目标分离。

The nullify statement disassociates a pointer from a target.

即使有多个指针指向同一目标,也不清空目标。不过,清空指针也暗示着无效化。

Nullify does not empty the targets as there could be more than one pointer pointing to the same target. However, emptying the pointer implies nullification also.

Example 1

以下示例演示了这些概念 −

The following example demonstrates the concepts −

program pointerExample
implicit none

   integer, pointer :: p1
   integer, target :: t1
   integer, target :: t2

   p1=>t1
   p1 = 1

   Print *, p1
   Print *, t1

   p1 = p1 + 4
   Print *, p1
   Print *, t1

   t1 = 8
   Print *, p1
   Print *, t1

   nullify(p1)
   Print *, t1

   p1=>t2
   Print *, associated(p1)
   Print*, associated(p1, t1)
   Print*, associated(p1, t2)

   !what is the value of p1 at present
   Print *, p1
   Print *, t2

   p1 = 10
   Print *, p1
   Print *, t2

end program pointerExample

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

1
1
5
5
8
8
8
T
F
T
0
0
10
10

请注意,每次运行代码时,内存地址都将不同。

Please note that each time you run the code, the memory addresses will be different.

Example 2

program pointerExample
implicit none

   integer, pointer :: a, b
   integer, target :: t
   integer :: n

   t = 1
   a => t
   t = 2
   b => t
   n = a + b

   Print *, a, b, t, n

end program pointerExample

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

2  2  2  4