I have a complicated program that when I run it shows a seg fault. Now if I compile it using -Mbounds it does not show a seg fault. Any idea what is going on. The option that I use to compile it is
-fastsse
that causes the seg fault. Take that out and it compiles and runs with no seg fault. But put fastsse in and you must have -Mbounds in also as a compiler option because then it will give a seg fault when it runs if no - Mbound is not in. What is going on?
THX 1138
Hi THX 1138,
When I see these types of issues, the first thing I try is compiling with “-O0 -g” and using Valgrind (ww.valgrind.org) to check for UMRs or out-of-bounds errors. Often code will ‘work’ with these errors but fail once optimization is applied.
I already compile withh -Mbounds. Should not that check for array bounds?
THX 1138
Should not that check for array bounds?
Yes, if the bounds are known.
What a minute. If the bounds are known why use -Mbounds at all?
THX 1138
Well, if you are using assumed-size arrays, -Mbounds might not have any idea what to do since it’s at the mercy of the programmer in that case. Of course, no one should ever use those any more, but they just keep popping up every now and then.
But, yeah, as Mat said, -Mbounds with -O0 and -g is always a good idea with a segfault. It’s so nice to get that message saying you’re trying to access element 0 of an array with a lower bound of 1, say. (You might need Valgrind for UMRs since I don’t think PGI has an option like Intel’s ‘-check uninit’. Or, do as I often do and just initialize every freaking array upon getting a segfault.)
Matt
What a minute. If the bounds are known why use -Mbounds at all?
If the bounds are not known, there is no way to check for being out of bounds. However, the bounds information available at a certain place may be inadequate or incorrect.
Here is an example that I constructed to show you that compiler bounds checking can only do so much, as TheMatt has pointed out above.
program buggy
integer, allocatable :: x(:)
allocate (x(3))
x = (/ 1, 2, 3 /)
call mysub(x,4) ! <== bug here, x has bounds 1:3
print *, x
contains
subroutine mysub(v, n)
integer :: n, v(n)
do i=2,n
v(i) = v(i-1)+v(i)
end do
return
end subroutine mysub
end program buggy
With or without -Mbounds, this program appears to run fine, but the bug is definitely present. Some day, perhaps as part of a larger program, this code can cause a seg-fault.