Segfault: is it a bug?
Paul Thomas
paulthomas2@wanadoo.fr
Wed Feb 14 21:11:00 GMT 2007
Salvatore,
Changing your main program to:
call create_mesh(msh,input_file,'MESH')
call create_field(quality,msh)
print *, "here" ! without this => seg fault
mshp => msh_(quality)
print *, size (mshp%area) ! correctly gives 10
I have seen something like this before - an I/O statement clearing a
segfault. Does anybody recall which PR it was?
Paul
> Hi there,
> The attached source code generates a rather spectacular segfault. The
> funny thing is that if I change the access function msh_ to a
> subroutine, it works. My understanding is that this is a bug; any kind
> soul can confirm my code is standard conforming, in which case I will go
> open a PR?
>
> Thanks
> Salvatore
>
>
> [sfilippo@localhost NEMO]$ gfortran -v
> Using built-in specs.
> Target: i686-pc-linux-gnu
> Configured with: ../gcc-4.2-20070207/configure --prefix=/usr/local/gcc42
> --with-mpfr=/home/travel/GCC/BUILDS/mpfr
> --with-gmp-lib=/home/travel/GCC/BUILDS/gmp/lib/
> --with-gmp=/home/travel/GCC/BUILDS/gmp
> Thread model: posix
> gcc version 4.2.0 20070207 (prerelease)
> [sfilippo@localhost NEMO]$ gfortran -o test_pnt test_pnt.f90
> [sfilippo@localhost NEMO]$ ./test_pnt
> Segmentation fault
>
>
> ------------------------------------------------------------------------
>
> module class_mesh
> type mesh
> integer :: nbc
> integer :: ncd
> real(kind(1.d0)), allocatable :: area(:)
> real(kind(1.d0)), allocatable :: dist(:)
> real(kind(1.d0)), allocatable :: interp(:)
> end type mesh
> contains
> subroutine create_mesh(msh, infile,name)
> type(mesh), intent(out) :: msh
> character(len=*) :: infile,name
> allocate(msh%area(10))
> return
> end subroutine create_mesh
> end module class_mesh
>
> module class_field
> use class_mesh
> implicit none
>
> private ! Default
> public :: create_field, field
> public :: msh_
>
>
> type field
> private
> character(len=32) :: name
> type(mesh), pointer :: msh => null()
> logical :: on_faces = .false.
> integer :: isize(2)
> end type field
>
> interface msh_
> module procedure msh_
> end interface
> interface create_field
> module procedure create_field
> end interface
> contains
> subroutine create_field(fld,msh,on_faces)
> type(field), intent(out) :: fld
> type(mesh), intent(in), target :: msh
> logical, intent(in), optional :: on_faces
>
>
> fld%msh => msh
>
> if(present(on_faces)) fld%on_faces = on_faces
> fld%name = 'Temp'
> fld%isize = 1
>
> end subroutine create_field
>
>
> function msh_(fld)
> type(mesh), pointer :: msh_
> type(field), intent(in) :: fld
>
> msh_ => fld%msh
>
> end function msh_
> end module class_field
>
> module class_scalar_field
>
> use class_field
>
> implicit none
>
> private ! Default
> public :: create_field, scalar_field
> public :: msh_
>
>
> type scalar_field
> private
> type(field) :: base
> real(kind(1.d0)), allocatable :: x(:)
> real(kind(1.d0)), allocatable :: bx(:)
> real(kind(1.d0)), allocatable :: x_old(:)
> end type scalar_field
>
> interface create_field
> module procedure create_scalar_field
> end interface
>
> interface msh_
> module procedure get_scalar_field_msh
> end interface
>
> contains
>
> subroutine create_scalar_field(fld,msh,on_faces)
> use class_mesh
>
> ! Mandatory arguments
> type(scalar_field), intent(out) :: fld
> type(mesh), intent(in), target :: msh
> logical, intent(in), optional :: on_faces
> !
> integer :: info, nel, nbf, isize(2)
> real(kind(1.d0)) :: x0_
>
>
> ! Creates the base-class member
> if (present(on_faces)) then
> call create_field(fld%base,msh,on_faces=on_faces)
> else
> call create_field(fld%base,msh)
> end if
>
> allocate(fld%x(10),fld%bx(20),stat=info)
> if(info /= 0) then
> write(0,*) 'Error from ALLOCATE in create_scalar_field'
> end if
>
> end subroutine create_scalar_field
>
> function get_scalar_field_msh(fld)
> use class_mesh
> !
> type(mesh), pointer :: get_scalar_field_msh
> type(scalar_field), intent(in), target :: fld
>
> get_scalar_field_msh => msh_(fld%base)
>
> end function get_scalar_field_msh
> end module class_scalar_field
>
> program test_pnt
> use class_mesh
> use class_scalar_field
>
> implicit none
> !
> character(len=30), parameter :: input_file = 'nemo.inp'
> !
> type(mesh) :: msh
> type(mesh), pointer :: mshp
> type(scalar_field) :: quality
> integer, allocatable :: bad_cells(:)
> real(kind(1.d0)) :: tol
>
>
>
> call create_mesh(msh,input_file,'MESH')
> call create_field(quality,msh)
> mshp => msh_(quality)
>
>
> end program test_pnt
>
More information about the Fortran
mailing list