Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28456 Discussions

Dummy array size (fake dynamic mem alloc in fortran77)

wolfpackNC
Beginner
456 Views
I'm guessing that this is clearly not good code??? But yet it compiles and runs. If n gets to big then we get seg fault (core dump)


but, Will somebody explain how I am able to create array 'c' of variable size in fortran 77 and store values in it. Should this be illegal?



program foo2
implicit none

integer n

n=300

call bar(n)

end program


subroutine bar(m)
implicit none

real*8 c(m,m)
integer m,i,j

do i=1,m
do j=1,m
c(i,j)=2.0d0
enddo
enddo

do i=1,m
do j=1,m
write(*,*)'HOW??',c(i,j)
enddo
enddo

end subroutine
0 Kudos
4 Replies
tom_p
Beginner
456 Views
That is probably because you run out of stack space. You could increase stack size (bash: ulimit -s hard), or compile with the -heap-arrays compiler option, or use allocatable arrays (not in Fortran 77).
0 Kudos
TimP
Honored Contributor III
456 Views
The automatic array extension was not part of Fortran 77, although several compilers supported it (some with very limited size). Fortran 77 didn't require compilers to have a means for flagging extensions they supported.
Nesting the loops backwards was bad in all versions of Fortran; even a few F77 compilers might have tried to fix this for you.
Also, yes, it's better style to use ALLOCATE with an appropriate error message in case of failure, which will tell you immediately where the fault occurred, should you exceed stack limits in a real program. You'll have difficulty with useability of any compiler which lies in the transition between f77 and f90 where automatic was supported but allocatable was not. Yes, I know some such may be left over from a decade ago.
0 Kudos
wolfpackNC
Beginner
456 Views
Thanks for all the replies! Yes, i would normally be using 95+ and use ALLOCATE/DEALLOCATE but I just came across this and was wondering if it was ripe for producing undesirable behavior, e.g. stack corruption.

Thanks!
0 Kudos
jimdempseyatthecove
Honored Contributor III
456 Views
This will not cause stack corruption, unless your code writes out of bounds of the dummy array. A segment fault (running out of stack) is not stack corruption - it is a resource limitation.

Jim Dempsey
0 Kudos
Reply