【发布时间】:2019-12-27 14:13:02
【问题描述】:
假设我有类似的东西:
real, dimension(:), allocatable :: S
integer, dimension(:) :: idx
...
S = S(idx)
S 和 idx 在分配前已正确分配/初始化。
Fortran 标准对S 的内存位置(地址)有何规定(如果有的话)?分配后它应该留在同一个地方吗?是否未指定(由编译器决定)?如果S 不是allocatable 有区别吗?
完整示例:
$ cat test.f90
program test
implicit none
real, dimension(:), allocatable :: S
integer :: i, idx(7) = [1,3,5,7,2,4,6]
allocate(S(size(idx)))
do i=1,size(S)
S(i) = i*i
end do
write(6,*) S
write(6,*) loc(S)
S = S(idx)
write(6,*) S
write(6,*) loc(S)
S(:) = S(idx)
write(6,*) S
write(6,*) loc(S)
deallocate(S)
end program
$ sunf90 -V
f90: Studio 12.6 Fortran 95 8.8 Linux_i386 2017/05/30
$ sunf90 test.f90 ; ./a.out
1.0 4.0 9.0 16.0 25.0 36.0 49.0
37518752
1.0 9.0 25.0 49.0 4.0 16.0 36.0
37519840
1.0 25.0 4.0 36.0 9.0 49.0 16.0
37519840
(假设loc给出了与数组地址相关的内容)
【问题讨论】:
标签: fortran dynamic-memory-allocation