This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
Re: checking array bounds for actual arguments
- From: Tobias Burnus <burnus at net-b dot de>
- To: Janus Weil <jaydub66 at googlemail dot com>
- Cc: "fortran at gcc dot gnu dot org" <fortran at gcc dot gnu dot org>
- Date: Tue, 25 Sep 2007 18:04:25 +0200
- Subject: Re: checking array bounds for actual arguments
- References: <854832d40709250808l4bc932a7p97e5a888af5a311d@mail.gmail.com>
Hi Janus,
Janus Weil wrote:
> I have a question regarding the following code sample:
>
> real x(2),y(3),z(4)
> subroutine a(x)
> real x(3)
>
> For the first call of the subroutine I get this warning:
> call a(x)
> 1
> Warning: Actual argument contains too few elements for dummy argument
> 'x' (2/3) at (1)
> But for "call a(z)" I don't get any warning, although the shape of the
> array does not match either.
>
Well, A asks for 3 real values (= 12 bytes); with "call a(x)" you only
pass 2 (8 bytes), which is one real value (4 bytes) too few. Using "call
a(z)" you pass 4 real values (16 bytes) of which the subroutine can use
the first 3 reals (12 bytes), which is ok.
Similarly, "call a(x(1))" and "call a(z(2))" are too small (2 reals)
whereas "call a(y(1))", "call a(z(1))" and "call a(z(2))" are ok.
See "12.4.1.5 Sequence association" in the Fortran 2003 standard:
"An actual argument represents an element sequence if it is an array
expression, an array element designator, a scalar of type default
character, or a scalar of type character with the C character kind
(15.1.1). If the actual argument is an array expression, the element
sequence consists of the elements in array element order. If the actual
argument is an array element designator, the element sequence consists
of that array element and each element that follows it in array element
order."
[...]
"An actual argument that represents an element sequence and corresponds
to a dummy argument that is an array is sequence associated with the
dummy argument if the dummy argument is an explicit-shape or
assumed-size array. The rank and shape of the actual argument need not
agree with the rank and shape of the dummy argument, but the number of
elements in the dummy argument shall not exceed the number of elements
in the element sequence of the actual argument."
Thus "call a(x)" is invalid as one violates: "the number of elements in
the dummy argument shall not exceed the number of elements in the
element sequence of the actual argument".
It is not necessarily undefined as long as one uses only the first two
elements of the dummy.* (Note: gfortran issues always a warning and not
an error as I could not figure out when to allow it and when not --
using other compilers, it depends on the exact usage whether no warning,
only a warning or an error is produced.)
(* Exciting (exciting.sf.net) is a program which does so: One of its
subroutines asks for N elements, but only uses N-1 elements; using NAG
f95 one gets an out-of-bounds run-time error as - at least once - only
N-1 elements are passed.)
> Is there any way to make gfortran issue a warning for such a case? And if not, do think it would make sense to
> implement such a check?
>
No, there is currently no way to make gfortran issuing a warning in this
case; it might be useful in some cases, but in many it is not. I would
not oppose having it as additional option, but it should not be enabled
by default.
Tobias