Why Does My Fortran 90 Function Cause a Segmentation Fault?

  • Context: Fortran 
  • Thread starter Thread starter yairsuari
  • Start date Start date
  • Tags Tags
    Fortran Function
Click For Summary
SUMMARY

The segmentation fault in the Fortran 90 function is likely caused by an attempt to access memory outside of allocated bounds or invoking an undefined subroutine. The function store_data is invoked with parameters that may not be properly defined, leading to memory access violations. To resolve this issue, it is essential to utilize debugging options in the compiler or linker to identify missing modules and check array bounds during execution. This approach will help pinpoint the exact cause of the segmentation fault.

PREREQUISITES
  • Understanding of Fortran 90 syntax and structure
  • Familiarity with NetCDF file operations
  • Knowledge of debugging techniques in Fortran compilers
  • Experience with memory management and array handling in Fortran
NEXT STEPS
  • Explore debugging options in Fortran compilers, such as gfortran's -g flag for debugging symbols
  • Learn about array bounds checking in Fortran using compiler flags like -fcheck=all
  • Investigate the use of tools like Valgrind for memory error detection in Fortran programs
  • Review best practices for defining and using optional parameters in Fortran subroutines
USEFUL FOR

This discussion is beneficial for Fortran developers, particularly those working with scientific computing and data storage in NetCDF files, as well as programmers seeking to improve their debugging skills in Fortran applications.

yairsuari
Messages
7
Reaction score
0
i have a fortran 90 function invoked:
iret = store_data(ncid_bfm,var_ids(n),OCET_SHAPE,NO_BOXES,garray=D3DIAGNOS(i,:))
each time i get to that line i get segmentation fault

on the calling unit variables a defined like so:
!BOP
!
! !IROUTINE: Store the results
!
! !INTERFACE:
subroutine save_bfm(time)
!
! !DESCRIPTION:
! output of BFM variables
!
! !USES:
use mem, only: D3STATE,D3DIAGNOS,D2STATE,D2DIAGNOS
implicit none
!
! !INPUT PARAMETERS:
REALTYPE,intent(in) :: time
! !LOCAL VARIABLES:
integer :: iret
integer :: i,j,k,n
REALTYPE :: temp_time

and on the invoked function the definitions are:
integer function store_data(ncid,id,var_shape,nbox, &
iscalar,iarray,scalar,array,garray, &
array2d,array3d)
!
! !DESCRIPTION:
! This routine is used to store a variable in the NetCDF file.
! The subroutine uses {\tt optional} parameters to find out which data
! type to save.
!
! !USES:
IMPLICIT NONE
!
! !INPUT PARAMETERS:
integer, intent(in) :: ncid,id,var_shape,nbox
integer, optional :: iscalar
integer, optional :: iarray(1:nbox)
REALTYPE, optional :: scalar
REALTYPE, optional :: array(1:nbox)
REALTYPE, optional :: garray(1:nbox)
REALTYPE, optional :: array2d(:,:)
REALTYPE, optional :: array3d(:,:,:)
!
! !REVISION HISTORY:
! Original author(s): Karsten Bolding & Hans Burchard
! Modifications: Marcello Vichi
!
!EOP
!
! !LOCAL VARIABLES:
integer :: iret,n=0
integer :: idum(1:nbox)
REAL_4B :: r4,dum(1:nbox)
!


my question is what can i do to prevent this fault??
 
Technology news on Phys.org
A segmentation fault usually means that you are trying to read or write to a memory location that you do not have the permission to do so. This usually translates to an array out of bourds situation, or calling a subroutine that is undefined. In certain cases, the memory location of the program has been inadvertently by data, and the program goes haywire.
As your problem is not intermittent, my first guess is you have an undefined subroutine, or a jump to an undefined location.
See if your compiler/linker has debug options to detect missing modules.
If that is not the case, see if you have debug options to check array bounds during execution. It slows down execution, but gives you a better chance to find your problem.
The definition of the variables gives very little hint. It is the program structure or the usage of arrays, that is causing the problem.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
25K
  • · Replies 5 ·
Replies
5
Views
13K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
11K
  • · Replies 3 ·
Replies
3
Views
3K