HP Fortran Programmer's Guide (September 2007)

Debugging
Handling runtime exceptions
Chapter 4122
This exception can also occur if your program is linked to a bad library, especially if the
library contains code that was written in assembler or if it was corrupted during a file
transfer.
This exception may indicate a compiler error. If you cannot find the cause of this exception in
your code, contact your HP support representative.
Segmentation violation exception
Before a program starts to execute, it is allocated a memory segment, which defines the area
of memory that it can use. If the program attempts to access a memory location outside its
segment, the operating system will raise the SIGSEGV signal, indicating a segmentation
violation or memory fault.
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 181.)
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.
1. See his Upgrading to Fortran 90 (New York 1995), p. 278.