主题:[讨论]关于可变数组在子程序中allocate的问题
			 gulley
				 [专家分:10]  发布于 2010-10-12 23:23:00
 gulley
				 [专家分:10]  发布于 2010-10-12 23:23:00							
			我在主程序中定义了一个可变数组,通过哑元形式在子程序中allocate,编译时,编译器会提示: warning #8055: The procedure has a dummy argument that has the ALLOCATABLE, ASYNCHRONOUS, OPTIONAL, POINTER, TARGET, VALUE or VOLATILE attribute. Required explicit interface is missing from original source.   [AA]
运行后,结果还是正确的。我用的是IVF编译器,代码如下。
我想知道:
1. 提示信息是什么意思?
2. 可变数组是不是不能通过哑元形式传递?
program f
    implicit none
    integer i,n
    real,allocatable::aa(:)
    write(*,*) 'please input an integer'
    read(*,*) i 
   
    call test(aa,i)
    write(*,*) (aa(n),n=1,i)
  
stop
end 
!----------------------------------------------------------
subroutine test(aa,i)
    implicit none
    real,allocatable::aa(:)
    integer i,n
    
    allocate(aa(i))
    aa(1)=100
    do n=1, i
        aa(n)=aa(1)*n*2
    end do
end subroutine test
						
					 
		
			
回复列表 (共8个回复)
		
								
				沙发
				
					 jstzhurj [专家分:4680]  发布于 2010-10-12 23:36:00
jstzhurj [专家分:4680]  发布于 2010-10-12 23:36:00				
				
需要一个显式的接口!
program f
    implicit none
    integer i,n
    real,allocatable::aa(:)
    write(*,*) 'please input an integer'
    read(*,*) i 
   
    call test(aa,i)
    write(*,*) (aa(n),n=1,i)
contains
 
subroutine test(aa,i)
    implicit none
    real,allocatable::aa(:)
    integer i,n
    
    allocate(aa(i))
    aa(1)=100
    do n=1, i
        aa(n)=aa(1)*n*2
    end do
end subroutine test
end 
							 
						
				板凳
				
					 yeg001 [专家分:14390]  发布于 2010-10-13 00:40:00
yeg001 [专家分:14390]  发布于 2010-10-13 00:40:00				
				这个问题似乎前几天才见过. 楼主的用法需要fortran2003才支持. 所以却决于你编译器是否足够新.
请问楼上, 写成内部函数fortran95也可以吗? 如果写成内部函数其实a和i都可以不传递直接用的.
							 
						
				3 楼
				
					 asymptotic [专家分:16630]  发布于 2010-10-13 07:13:00
asymptotic [专家分:16630]  发布于 2010-10-13 07:13:00				
				二楼 yeg001 说得有道理,其实楼主对 Allocatable Dummy Argument 理解程度不够。撇开 Internal procedure -- Host association 不谈,若是你的子例程传递参数 i 的话,没有必要用 Allocatable Dummy Argument。
							 
						
				4 楼
				
					 lqmj [专家分:40]  发布于 2010-10-25 20:08:00
lqmj [专家分:40]  发布于 2010-10-25 20:08:00				
				根据3楼的观点我将1楼的程序做了一点改动。compile,build,execute都通过,屏幕上显示:input the integer,我输入3后,出现:
    应用程序错误
“0x00401041”指令引用的“0x00000000”内存。该内存不能为“written”.
郁闷的是不知我的电脑出现了什么问题,感谢各位高手能告知。
							 
						
				5 楼
				
					 aliouying [专家分:1150]  发布于 2010-11-10 00:14:00
aliouying [专家分:1150]  发布于 2010-11-10 00:14:00				
				3楼都意思不是很理解。
这个意思?
program f
    implicit none
    integer i,n
    real,allocatable::aa(:)
    write(*,*) 'please input an integer'
    read(*,*) i 
       allocate(aa(i))
    call test(aa,i)
    write(*,*) (aa(n),n=1,i)
contains
 
subroutine test(aa,i)
    implicit none
    
    integer i,n
    real::aa(i)
    
    aa(1)=100
    do n=1, i
        aa(n)=aa(1)*n*2
    end do
end subroutine test
end 
							 
						
				6 楼
				
					 asymptotic [专家分:16630]  发布于 2010-11-10 08:21:00
asymptotic [专家分:16630]  发布于 2010-11-10 08:21:00				
				5 楼 aliouying 网友:您理解的没错,对于 subroutine test 参数 i 似乎没有必要。
							 
						
				7 楼
				
					 baccle [专家分:190]  发布于 2010-11-10 11:42:00
baccle [专家分:190]  发布于 2010-11-10 11:42:00				
				1楼的说法似乎有问题,这么写十有八九运行不对。
从主程序传入子程序的数组,在子程序中不能是allocate属性,只能是假定形状数组
subroutine test(aa,i)
    implicit none
    real::aa(:)
    integer i,n
……
end subroutine test
然后在主程序里写上接口,接口必须要写,就对了。
动态数组和假定形状数组是完全不同的两回事,虽然形状差不多
							 
						
				8 楼
				
					 aliouying [专家分:1150]  发布于 2010-11-10 19:59:00
aliouying [专家分:1150]  发布于 2010-11-10 19:59:00				
				[quote]5 楼 aliouying 网友:您理解的没错,对于 subroutine test 参数 i 似乎没有必要。[/quote]
直接复制的内部函数,如果是外部过程的话,还是需要传入的。
							 
									
			
我来回复