@@ -46,6 +46,16 @@ deallocate(pa)
46
46
```
47
47
- 需要注意的是,** 在使用函数的时候,作为局部变量,可分配变量离开了作用域会自动释放,但是指针不会**
48
48
49
+ 使用` associated ` 可以查看指针的关联属性
50
+ ``` fortran
51
+ integer,pointer::pa(:)
52
+ integer,allocatable,target::a(:)
53
+ allocate(a(3),source=[1,2,3])
54
+ pa=>a
55
+ write(*,*)associated(pa) !检查pa是否被关联
56
+ write(*,*)associated(pa,a)!检查pa是否和a关联
57
+ ```
58
+
49
59
## 内存泄漏
50
60
51
61
如果对指针进行了分配,但是同时有用它指向了其他的内存,那么就会出现内存泄漏。
@@ -81,15 +91,7 @@ pa=>null()
81
91
```
82
92
83
93
## 关联(` associate ` )
84
- 使用` associated ` 可以查看指针的关联属性
85
- ``` fortran
86
- integer,pointer::pa(:)
87
- integer,allocatable,target::a(:)
88
- allocate(a(3),source=[1,2,3])
89
- pa=>a
90
- write(*,*)associated(pa) !检查pa是否被关联
91
- write(*,*)associated(pa,a)!检查pa是否和a关联
92
- ```
94
+
93
95
Fortran2003中引入了` associate ` 语句,可以简化在代码编写过程中的临时变量问题,基本的语法是
94
96
``` fortran
95
97
integer::point(3)
@@ -99,7 +101,7 @@ associate(x=>point(1),y=>point(2),z=>point(3))
99
101
l=x**2+y**2+z**2
100
102
end associate
101
103
```
102
- 需要注意的是,此时,如果` x=>p ` 的右边是变量,** 那么` x ` 相当于是` p ` 的别名,修改了` x ` ,` p ` 也会随之改变** 。如果右边是变量,那么相当于 ** 自动创建一个临时变量 ** 。
104
+ 需要注意的是,此时,如果` x=>p ` 的右边是变量,** 那么` x ` 相当于是` p ` 的别名,修改了` x ` ,` p ` 也会随之改变。如果右边是表达式,那么相当于自动创建一个临时变量 ** 。
103
105
``` fortran
104
106
integer::a(10)
105
107
integer::i
0 commit comments