Search code examples
segmentation-faultfortrangfortrandynamic-allocation

Fortran thinks non allocated array is already allocated


I am facing the following problem and can not figure out what is happening.

I have a routine that allocates some working arrays in the beginning of my code. These working arrays are part of data structure.

The structure is defined as:

 Type bond_stat
  Real*8,Allocatable,Dimension( : , : ) ::data_space
  Real*8,Allocatable,Dimension( : , : ) ::data_time
  Real*8,Allocatable,Dimension( : )     ::bin_width         
  Real*8,Allocatable,Dimension( : )     ::time
  Integer,Allocatable,Dimension( : , : )  ::connection_table
 End Type bond_stat
 Type( bond_stat ),allocatable,dimension( : ) ::Hbonds

This is done inside a module. The next step what I am doing is to allocate this structure as

Allocate( Hbonds( 1:2 ) )

Then I call a subroutine where the arrays that are contained in the structure are allocated. The strange thing is now if I try to allocate the arrays with

 Allocate( Hbonds( i )%data_space( 1 : 3 , 1 : Nbins ) )
 Allocate( Hbonds( i )%data_time( 1 : 1 , 1 : Nbins ) )
 Allocate( Hbonds( i )%time( 1 : N ) )
 Allocate( Hbonds( i )%bin_width( 1 : 2 ) )
 Allocate( Hbonds( i )%connection_table( 1 : 2 , 1 : N ) ) )

If I do so the code states while execution the time array for example is already allocated. I 100% sure I do not allocate this array before. I am also sure the array is not used before and maybe the compiler allocates it since it is already in use. So I thought I would make a check if the array is already allocated and if it is allocated then deallocate it and reallocate it. If I do so I receive a segmentation fault in the deallocation line:

    If ( Allocated( Hbonds( i )%time ) ) then
       Deallocate( Hbonds( i )%time )
    End If
    Allocate( Hbonds( i )%time( 1 : N ) )

I am completely puzzled what is going on here. Please can anybody help. The output looks as:

Program received signal SIGSEGV: Segmentation fault - invalid memory 
reference.
Backtrace for this error:
#0  0x2b54685d3f0d in ???
#1  0x2b54685d314d in ???
#2  0x2b5468e4acaf in ???
#3  0x2b5468e9715c in ???
#4  0x4d27cf in __bond_statistics_MOD_bond_stat_init
at/fs/home/jona/programs/gfortran/eval_new01/playground/H_bond_analysis.f90:35
#5  0x4c8f5f in __bond_statistics_MOD_prep_bond_stat
at/fs/home/jona/programs/gfortran/eval_new01/playground/H_bond_analysis.f90:
350
#6  0x44477a in ???
#7  0x446724 in ???
#8  0x2b5468e35f44 in ???
#9  0x4016c8 in ???
#10  0xffffffffffffffff in ???
Segmentation fault (core dumped)

And when not trying to deallocate the array before it looks like:

At line 38 of file H_bond_analysis.f90
Fortran runtime error: Attempting to allocate already allocated variable 'hbonds'
Error termination. Backtrace:
#0  0x2ae8991bef0d in ???
#1  0x2ae8991bfa45 in ???
#2  0x2ae8991bfdfa in ???
#3  0x4d28e5 in __bond_statistics_MOD_bond_stat_init
at/fs/home/jona/programs/gfortran/eval_new01/playground/H_bond_analysis.f90:38
#4  0x4c8f5f in __bond_statistics_MOD_prep_bond_stat
at/fs/home/jona/programs/gfortran/eval_new01/playground/H_bond_analysis.f90:350
#5  0x44477a in ???
#6  0x446724 in ???
#7  0x2ae899a20f44 in ???
#8  0x4016c8 in ???
#9  0xffffffffffffffff in ???

with line 38 being the line where time is allocated. I am using gfortran compiler with flags FLAGS = -fopenmp -g -Wall -fcheck=all -fbounds-check


Solution

  • I figured now out where the problem was. I am using a makefile to compile the code. Today I wanted to add some more arrays to the bond_stat module and after executing the makefile I ran into the same error again. What I did now is to recompile all the modules by hand without changing anything in the code itself. This solved the problem.