Re: Q: Checking the size of a non-allocated array?



James Tursa <aklassyguy@xxxxxxxxxxx> wrote:

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?

Yes what you are doing is nonstandard. But the problem is more
fundamental than you are thinking. passing an unallocated allocatable as
an actual argument is already invalid (unless the dummy argument is
allocatable, which requires either the F95 TR or f2003). That call being
invalid in the first place, by the time you get to the subroutine, it is
too late.

Absolutely the only way that it is valid to pass an unallocated
allocatable as an actual argument is if the dummy is allocatable. But if
the dummy is allocatable, then the actual always has to be allocatable.

I don't know enough about the context to know what to suggest, but the
direction you seem to be looking towards won't work.

If it fits your application, one approach might be to allocate the
allocatable as size zero. That is then perfectly fine. There are some
people who get confused about the distinction between an array being
allocated to size 0 and not being allocated. There is a distinction and
it is important. It might concievably help your application.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
.



Relevant Pages