Q: Checking the size of a non-allocated array?
- From: James Tursa <aklassyguy@xxxxxxxxxxx>
- Date: Fri, 10 Aug 2007 04:36:07 GMT
Running Windows XP, Intel Pentium 4, Intel 9.1 compiler
I have written some code that checks the size of a passed array as
shown below. For a fixed size array (the first call from myprog), the
subroutine mysub will call size(x,1) and get a legitimate non-zero
result. Once I know there is an array I can legitimately access, then
I can fill it with something. For an allocatable array that is not
currently allocated (the second call from myprog), subroutine mysub
will call size(x,1) and get zero (at least with my setup). This lets
the subroutine know not to try to access the elements, and it will
return without doing anything. Problem is, the documentation I have
been reading says size(x,1) for an unallocated array x is undefined.
So I suspect I am doing something that works with my setup but is not
part of the standard. Is this true? And if so, then how can I code
the subroutine to know if the passed array can be accessed? I would
like to code the subroutine to gracefully check for this type of
problem, but if I can't use size() then how do I do it?
module mymodule
contains
subroutine mysub(x)
!-ARG
integer x(:)
!-----
if( size(x,1) /= 0 ) then ! how do I know this will work?
x(1) = 1
endif
return
end subroutine mysub
end mymodule
!************************************
program myprog
use mymodule
!-LOC
integer y(5)
integer, allocatable :: x(:)
!-----
call mysub(y) ! this call should be ok
call mysub(x) ! this call invokes size(x,1), which may not be
ok
! rest of the code
end program myprog
.
- Follow-Ups:
- Re: Q: Checking the size of a non-allocated array?
- From: Arno
- Re: Q: Checking the size of a non-allocated array?
- From: Richard Maine
- Re: Q: Checking the size of a non-allocated array?
- Prev by Date: Re: testing for floating point equality
- Next by Date: Re: Q: Checking the size of a non-allocated array?
- Previous by thread: Reverse order of data in a particular dimension
- Next by thread: Re: Q: Checking the size of a non-allocated array?
- Index(es):
Relevant Pages
|