| avoiding out of bounds errors |
|
 |
|
 |
|
 |
|
 |
Group: comp.lang.fortran · Group Profile
Author: BeliavskyBeliavsky Date: Aug 16, 2007 12:48
No question here, just a comment.
The Fortran standard handles zero-sized arrays in a manner that is
consistent with other arrays, but a programmer still needs to think
about whether if-branches are needed for zero-sized arrays in his
code.
The following code crashes if i1(:) has zero size after calling
change_pos. I discovered this by accidentally running a program with
an empty data file. After fixing one out-of-bounds error, another
turns up. Eventually I will add a branch to stop the program when an
empty data file is encountered, but meanwhile I am finding bugs in
lots of places :(. In the future, I should test all procedures with
zero-sized arrays.
subroutine group_ranges(ivec,i1,i2)
! return in i1(:) and i2(:) ranges of ivec that have equal values
integer, intent(in) :: ivec(:)
integer, intent(out), allocatable :: i1(:),i2(:)
integer :: i,ngroups
call change_pos(ivec,i1)
ngroups = size(i1)
allocate (i2(ngroups))
do i=1,ngroups-1
i2(i) = i1(i+1) - 1
end do
i2(ngroups) = size(ivec)
end subroutine group_ranges
|