Skip to content

Commit 3f3e948

Browse files
authored
Update ch08-00-pointer.md
1 parent e1e4e57 commit 3f3e948

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/ch08-00-pointer.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ deallocate(pa)
4646
```
4747
- 需要注意的是,**在使用函数的时候,作为局部变量,可分配变量离开了作用域会自动释放,但是指针不会**
4848

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+
4959
## 内存泄漏
5060

5161
如果对指针进行了分配,但是同时有用它指向了其他的内存,那么就会出现内存泄漏。
@@ -81,15 +91,7 @@ pa=>null()
8191
```
8292

8393
## 关联(`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+
9395
Fortran2003中引入了`associate`语句,可以简化在代码编写过程中的临时变量问题,基本的语法是
9496
``` fortran
9597
integer::point(3)
@@ -99,7 +101,7 @@ associate(x=>point(1),y=>point(2),z=>point(3))
99101
l=x**2+y**2+z**2
100102
end associate
101103
```
102-
需要注意的是,此时,如果`x=>p`的右边是变量,**那么`x`相当于是`p`的别名,修改了`x``p`也会随之改变**。如果右边是变量,那么相当于**自动创建一个临时变量**
104+
需要注意的是,此时,如果`x=>p`的右边是变量,**那么`x`相当于是`p`的别名,修改了`x``p`也会随之改变。如果右边是表达式,那么相当于自动创建一个临时变量**
103105
``` fortran
104106
integer::a(10)
105107
integer::i

0 commit comments

Comments
 (0)