【发布时间】:2020-02-01 01:24:26
【问题描述】:
这行不通
program main
implicit none
integer :: nx = 3
integer :: ny = 5
integer :: nz = 8
real, allocatable, dimension(:,:,:) :: A
real, allocatable, dimension(:,:) :: B
allocate(A(nx,0:ny,nz) )
! ...do something with array A and at some point cope a slice of A to B:
B = A(:,:,1)
! in this case B is (1:nx, 1: ny+1)
end program main
上面的代码自动分配 B 并将 A(:,:,1) 复制到 B。但是它不保持 0/ny 的下限/上限,而是 B 的下限为 1,上限为纽约+1。
我发现保持 A 2dn-dim 的下限/上限的唯一方法是将 B 显式分配为:
allocate(B(nx, 0:ny))
B = A(:,:,1)
! in this case B is (1:nx, 0:ny)
鉴于我有比这个简单示例中更多的变量,有没有办法像 B=A(:,:,1) 一样进行分配,并且在不显式分配 B 的情况下保持 A 的边界?
【问题讨论】:
-
我看到了同样的行为。这不是一个错误,而是一个偶然的功能。本质上,您使用
=运算符将一个数组复制到另一个具有不同边界的数组。 -
谢谢你 francescalus,是的,它确实回答了部分问题。
标签: multidimensional-array fortran bounds allocatable-array