This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: strange memory leak


Hi,

mattias alveteg wrote:
> I got runtime error messages when running some code, compiled with
> GFortran 4.2.0 (2006-10-15) containing the following code (inside a
> module)
>
> subroutine EvaluateStateError(states1,error)
I think the following complete program is equivalent:
--------------
program test
implicit none
real :: st(5,5)
st = 1.0
call EvaluateStateError(st)
contains
subroutine EvaluateStateError(states1)
real,dimension(:,:),intent(in) :: states1
integer :: i(1),j(1)
real :: Diff(size(states1,1),size(states1,2))
Diff = 0.0
i = maxloc(abs(Diff),dim=1)
j = maxloc(abs(Diff),dim=2)
end subroutine
end program test
--------------

With all warnings turned on it does not crash with ifort (-check all
-stand f95 -warn all; 9.1.037), but it does so with gfortran (-Wall
-fbounds-check -std=f95; 4.1.2 20060705).

If one replaces the 'i' with "print *,' the following is shown:
"1 1 1 1 1"

> Changing the last 3 lines to this:
>
> diff=abs(diff)
> error = maxval(diff)
> i=maxloc(diff,dim=1)
> j=maxloc(diff,dim=2)
> made the runtime error disappear.

If I change the program into
---------------
[...]
real ::
Diff(size(states1,1),size(states1,2)),tmp(size(states1,1),size(states1,2))
Diff = 0.0
tmp = abs(diff)
i = maxloc(tmp,dim=1)
j = maxloc(tmp,dim=2)
[...]
---------------
it still crashes here with gfortran.

I would assume that it is a out of boundary problem (and my gfortran is
too old to catch it; the hard disc of my workstation is broken and thus
I cannot test newer gfortrans, NAGf95 etc.).

What puzzles me, though, that ifort does not have an array-out-of-bounds
problem and that the temporary variable fixes the problem at
your/Mattias' side, where it does not do so here.

MAXLOC should return the following (from Fortran 2003,13.7.73):
"Result Characteristics. Integer. If KIND is present, the kind type
parameter is that specified by the value of KIND; otherwise the kind
type parameter is that of default integer type. If DIM is absent, the
result is an array of rank one and of size equal to the rank of ARRAY;
otherwise, the result is of rank n â 1 and shape (d1, d2, ..., dDIMâ1,
dDIM+1, ..., dn), where (d1, d2, ..., dn) is the shape of ARRAY."

* * *

By the way, if I make the size of st(:,:) is known at the compile time, e.g.
------------------------------
program test
implicit none
real :: st(5,5)
st = 1.0
call EvaluateStateError(st)
contains
subroutine EvaluateStateError(states1)
real,dimension(5,5),intent(in) :: states1
integer :: i(1),j(1)
real ::
Diff(size(states1,1),size(states1,2)),tmp(size(states1,1),size(states1,2))
Diff = 0.0
i = maxloc(abs(Diff),dim=1)
j = maxloc(abs(Diff),dim=2)
end subroutine
end program test
------------------------------

both compilers complain
------------------------------------------
fortcom: Error: test.f90, line 12: The shapes of the array expressions
do not conform. [I]
i = maxloc(abs(Diff),dim=1)
--^
------------------------------------------
i = maxloc(abs(Diff),dim=1)
1
Error: different shape for Array assignment at (1) on dimension 1 (1/5)
------------------------------------------

Tobias


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]