I am using an allocatable array to append an array of data onto it multiple times. Each time the data is added to the array, I am creating a pointer array to the added data in the array. I noticed that after the allocatable array size goes over 4, the next time I add data to the array, the pointer to the first four values in the array is a random large number.
This is only happening with the gfortran compiler. When I use the ifort compiler I do not have the same problem. At this point I plan on using a different method to build this array, however, I’m curious on why this is happening in the first place.
Example code is below.
program example
implicit none
integer, parameter :: nApp = 5
integer, dimension(:), allocatable, target :: TargetArray
integer, dimension(nApp), target :: AppendArray
integer, dimension(:), pointer :: p1, p2, p3
integer :: nn, ii
nn = nApp
allocate(TargetArray(0))
AppendArray = [(ii, ii = nn-(nApp-1), nn)]
TargetArray = [TargetArray, AppendArray]
p1 => TargetArray(nn-(nApp-1):nn);
print *, p1
nn = nn + nApp
AppendArray = [(ii, ii = nn-(nApp-1), nn)]
TargetArray = [TargetArray, AppendArray]
p2 => TargetArray(nn-(nApp-1):nn);
print *, p1
print *, p2
nn = nn + nApp
AppendArray = [(ii, ii = nn-(nApp-1), nn)]
TargetArray = [TargetArray, AppendArray]
p3 => TargetArray(nn-(nApp-1):nn);
print *, p1
print *, p2
print *, p3
print *, TargetArray
end program example
The output when using gfortran compiler:
1 2 3 4 5
995220502 0 2105687195 419310127 5
6 7 8 9 10
995220502 0 2105687195 419310127 5
6 7 8 9 10
11 12 13 14 15
1 2 3 4 5 6 7 8 9 10
The output when using ifort compiler:
1 2 3 4 5
1 2 3 4 5
6 7 8 9 10
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15