将未分配的数组传递给没有显式接口的例程有什么问题吗? [英] Is there anything wrong with passing an unallocated array to a routine without an explicit interface?

查看:49
本文介绍了将未分配的数组传递给没有显式接口的例程有什么问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑:

 程序主要真实,可分配,尺寸(:) :: foo整数nn = 10调用dofoo(foo,n,1)分配(foo(n))呼叫dofoo(foo,n,0)结束程序主要子程序dofoo(foo,n,mode)真正的foo(n)整数i,n,模式如果(mode.eq.1)然后n = 6返回万一我是否= 1,nfoo(i)= i尽头返回结束子程序dofoo 

上面的代码有什么问题吗?(它与gfortran一起使用)第一次传入未分配的数组,但我没有碰过-标准中是否有任何内容可能导致它以与系统有关的方式运行?

解决方案

您几乎已经回答了自己的问题.是的,按照标准,如果您在范围内没有接口,则将未分配的可分配数组作为实际参数传递始终是非法的.

如果您在作用域中具有接口,则仅当哑元参数也是可分配的时,这才是合法的.

是的,我被它咬了.我的解决方法是在通话前将大小分配为零.<​​/p>

Consider:

program main
real, allocatable, dimension(:) :: foo
integer n
n=10
call dofoo(foo,n,1)
allocate(foo(n))
call dofoo(foo,n,0)
end program main

subroutine dofoo(foo,n,mode)
real foo(n)
integer i,n,mode
if(mode.eq.1)then
   n=6
   return
endif
do i=1,n
   foo(i)=i
enddo
return
end subroutine dofoo

Is there anything wrong with the above code? (It works with gfortran) I pass in an un-allocated array the first time, but I don't touch it -- Is there anything in the standard that could cause this to behave in a system dependent way?

解决方案

You've almost answered your own question. Yes, by the standard, it is always illegal to pass an unallocated allocatable arrays as an actual argument if you don't have an interface in scope.

If you have an interface in scope it is only legal if the dummy argument is also allocatable.

And yes I've been bitten by it. My work around has been to allocate to zero size before the call.

这篇关于将未分配的数组传递给没有显式接口的例程有什么问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆