已分配数组的初始状态

时间:2013-02-12 16:54:19

标签: static fortran

在fortran中,可以安全地假设未分配数组的状态为.not.allocated,并且如果使用save属性声明可调用数组之间的状态,则可以保留它们的状态吗?换句话说,除非出现轻微的输出格式差异,否则可以安全地假设以下程序将始终产生输出:

 First time here
 Been here before

测试程序:

  program main
    call sub()
    call sub()
  end program main

  subroutine sub()
    real,save,allocatable,dimension(:) :: a
    if(.not. allocated(a))then
       print*,"First time here"
       allocate(a(10))
    else
       print*,"Been here before"
    endif
  end subroutine sub

我问主要是因为我知道你不能假设指针的默认关联是.not.associated

2 个答案:

答案 0 :(得分:4)

是!

现在我发现你需要30个字符......

答案 1 :(得分:0)

是的,这是Fortrans可分配数组的好东西之一。但是,如果由于某些原因,你必须使用指针,你可以通过以下方式实现类似的效果:

program main
  call sub()
  call sub()
end program main

subroutine sub()
  real, pointer, dimension(:), save :: a => null()
  if(.not. associated(a))then
    print*,"First time here"
    allocate(a(10))
  else
    print*,"Been here before"
  endif
end subroutine sub

save属性在这里是可选的,因为变量声明中的赋值暗示了这一点。

相关问题