HP Fortran Programmer's Guide (B3908-90031; September 2011)

Debugging
Handling runtime exceptions
Chapter 4126
Any program that can generate address references outside its segment—for example, by indexing beyond
the declared boundary of an array—may cause a segmentation violation. In C programs, bad pointers often
result in this exception. The standard Fortran 90 pointer is more self-protective than the C pointer, but it too
can be misused and lead to the state of mind memorialized in the lyric (known only to Cooper Redwine
1
):
“I’ve got those segmentation violation, core dumped blues.” The Cray-style pointer extension is more like
the C pointer and is therefore more susceptible to the abuse that results in segmentation violations.
Programs that cause a stack overflow (for example, by attempting to allocate more local variables on the
stack than the kernel can handle or by infinite recursion) can also cause a segmentation violation. If your
program needs a bigger stack, run the System Administrator Manager (SAM) and increase the maxssiz
parameter. Also, see the HP-UX System Administration Tasks manual for information about reconfiguring
the kernel.
Segmentation violations are especially common when calling C functions from Fortran program units. If the
number, type, or calling conventions of the arguments being passed do not match, the call is likely to result
in an exception. For example, if you use the built-in function %VAL to declare an argument as passed by
value, but the C function is expecting a pointer, a segmentation violation may occur. (%VAL and %REF are
HP Fortran extensions; for information about using them when calling a C routine from Fortran, see
“Argument-passing conventions” on page 186.)
In most cases, debugging requires locating the code that caused the segmentation violation and rewriting it.
If your program aborts with this error, recompile it with the +fp_exception option. A program compiled
with this option will display a procedure traceback when it aborts. The procedure traceback lists procedure
names and offset addresses of the code that caused the exception.
If you suspect that an out-of-bounds array reference is causing the segmentation violation, you can use the
+check=all option instead of the +fp_exception option. When compiled with the +check=all
option, a program that attempts to reference an array element that is outside the declared array boundary will
abort with an error message that gives the line number of where the reference was detected.
The +check=all also performs runtime checks for out-of-bounds substrings and for integer overflow; see
“Calling a trap procedure” on page 132. The +check option is fully described in the HP Fortran
Programmer’s Reference.
Bad argument exception
This exception occurs when a bad argument (for example, an out-of-range argument) is passed to a kernel
system routine. This exception can also occur in programs that make explicit calls to the kernel threads
library, /usr/lib/libpthread.sl, and pass bad arguments.
1. See his Upgrading to Fortran 90 (New York 1995), p. 278.