Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28538 Discussions

Another question: Can I pass an unallocated array to a subroutine and allocate it in that program?

Zhanghong_T_
Novice
510 Views
Just like this:
integer, allocatable::test(:)
call allocatearray(test)
end
subroutine allocatearray(test)
integer, allocatable:: test(:)
if(allocated(test))deallocate(test)
allocate(test(100))
end subroutine
The program can be compiled and built, but when run it, it shows "Access Violation". How can I allocate an array in a subroutine (except for use a common module)?
Thanks,
Zhanghong, Tang
0 Kudos
4 Replies
Jugoslav_Dujic
Valued Contributor II
510 Views
This is supposed to work under Allocatable TR, implemented in CVF 6.5+ or IVF 8+. However, you
have to have explicit interface of AllocateArray in the caller -- either put it in a MODULE (best) or write an interface block.
Jugoslav
0 Kudos
Zhanghong_T_
Novice
510 Views
Thank you!
Can I realize it without use module? How can I realize it by interface?
Zhanghong, Tang
0 Kudos
Jugoslav_Dujic
Valued Contributor II
510 Views
Just write an INTERFACE block in the caller containing prototype of the actual routine:
interface
subroutine A(x)
real, allocatable:: x(:)
end subroutine A
end interface
Note that INTERFACE blocks are 1) clumsy and 2) error-prone, because if you e.g. add an argument to A, you have to rewrite all INTERFACE blocks as well or you will get strange run-time errors. Ideally, they should be used only when interfacing with routines written in other language (or for "cheating" purposes, as in my other post).
Jugoslav
0 Kudos
Zhanghong_T_
Novice
510 Views
Thank you again! This is what I want!
Zhanghong, Tang
0 Kudos
Reply