Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

optional argument of elemental function

4 views
Skip to first unread message

Beliavsky

unread,
Mar 9, 2007, 6:37:28 PM3/9/07
to
I don't see what is wrong with the following code, but none of the
compilers run it without crashing. Since ivec is not passed to sub, I
think the two calls to set_optional should be equivalent, but
compilers crash upon the second call.

module sub_mod
contains
elemental subroutine set_optional(i,idef,iopt)
! set i to (iopt,idef) if iopt (is,is not) PRESENT
integer, intent(out) :: i
integer, intent(in) :: idef
integer, intent(in), optional :: iopt
if (present(iopt)) then
i = iopt
else
i = idef
end if
end subroutine set_optional
!
subroutine sub(ivec)
integer , intent(in), optional :: ivec(:)
integer :: ivec_(2)
call set_optional(ivec_,(/1,2/))
print*,"ivec_=",ivec_
call set_optional(ivec_,(/1,2/),ivec)
print*,"ivec_=",ivec_
end subroutine sub
end module sub_mod

program main
use sub_mod, only: sub
call sub()
end program main

mingw g95 gcc version 4.0.3 (g95 0.91!) Mar 8 2007 says

ivec_= 1 2
At line 20 of file xopt_bug.f90
Traceback: (Innermost first)
Called from line 27 of file xopt_bug.f90
Fortran runtime error: Array element out of bounds: 3 in (1:2), dim=1

mingw gfortran gcc version 4.3.0 20061021 (experimental) says

Fortran runtime error: Array bound mismatch, size mismatch for
dimension 1 of array 'ivec' (in file 'xopt_bug.f90', at line 20)
ivec_= 1 2

Intel Visual Fortran Version 9.1 Build 20070109Z Package ID:
W_FC_C_9.1.034 says

ivec_= 1 2
forrtl: severe (157): Program Exception - access violation
Image PC Routine Line
Source
ifort_xopt_bug.ex 0040108C _MAIN__ 27
xopt_bug.f90
ifort_xopt_bug.ex 0044C038 Unknown Unknown Unknown
ifort_xopt_bug.ex 00431254 Unknown Unknown Unknown
kernel32.dll 7C816FD7 Unknown Unknown Unknown

Salford/Silverfrost 4.9.0 says something similar

The code compiles without warnings or errors with the Lahey Source
Check.

glen herrmannsfeldt

unread,
Mar 9, 2007, 7:06:04 PM3/9/07
to
Beliavsky wrote:
> I don't see what is wrong with the following code, but none of the
> compilers run it without crashing. Since ivec is not passed to sub, I
> think the two calls to set_optional should be equivalent, but
> compilers crash upon the second call.

It might be that you used enough features to confuse the
compiler (or compiler writers).

It was said recently that optional arguments can be passed
down to other subroutines, but doing it with an elemental routine
might just be enough to confuse the compiler, and doesn't seem
to offer a big advantage.

I would probably just use an assumed shape array with an optional
third array, instead of elemental.

(Snip of example of elemental subroutine with optional argument.)

-- glen

Rich Townsend

unread,
Mar 9, 2007, 8:49:54 PM3/9/07
to

I recall hitting a similar problem with IFC recently. Have you considered
upgrading to the latest version?

cheers,

Rich

Beliavsky

unread,
Mar 9, 2007, 10:03:09 PM3/9/07
to
On Mar 9, 6:37 pm, "Beliavsky" <beliav...@aol.com> wrote:

<snip>

> The code compiles without warnings or errors with the Lahey Source
> Check.

And with LF95 7.1 on Windows compiles and gives the output I
expected,

ivec_= 1 2
ivec_= 1 2

Lahey/Fujitsu almost always gets thing right!

The workaround is to test whether ivec is PRESENT in sub and call
set_optional accordingly.

0 new messages