2

(Edited 2021-09-06 13:30 CEST after I found out that the problem is caused at a different place of my code already)

I am working on some code (not my own) where they use a custom type for dynamic integer arrays. One instance of this type is passed via a common block between routines. It looks like this is not working properly or as expected, since the arrays take different values after being passed to a new routine. A minimal example looks like this:

implicit none
type intdynarr2
    sequence
    integer, pointer::i(:,:)
end type intdynarr2
type(intdynarr2):: my_array
integer j,k
common/carrays/my_array
allocate(my_array%i(1:10,1:10))
do j=1,10
  do k=1,10
    my_array%i(j,k)=j*k
  enddo
enddo
write(*,*) my_array%i
call printarr
end
      
subroutine printarr
    implicit none
    type intdynarr2
        sequence
        integer, pointer::i(:,:)
    end type intdynarr2
    type(intdynarr2):: my_array
    common/carrays/my_array
    write(*,*) my_array%i
end subroutine

The output of this after compiling with gfortran in version 8.2.0 looks like this:

           1           2           3           4           5           6           7           8           9          10           2           4           6           8          10          12          14          16          18          20           3           6           9          12          15          18          21          24          27          30           4           8          12          16          20          24          28          32          36          40           5          10          15          20          25          30          35          40          45          50           6          12          18          24          30          36          42          48          54          60           7          14          21          28          35          42          49          56          63          70           8          16          24          32          40          48          56          64          72          80           9          18          27          36          45          54          63          72          81          90          10          20          30          40          50          60          70          80          90         100
           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1           1

while I expected it to be:

           1           2           3           4           5           6           7           8           9          10           2           4           6           8          10          12          14          16          18          20           3           6           9          12          15          18          21          24          27          30           4           8          12          16          20          24          28          32          36          40           5          10          15          20          25          30          35          40          45          50           6          12          18          24          30          36          42          48          54          60           7          14          21          28          35          42          49          56          63          70           8          16          24          32          40          48          56          64          72          80           9          18          27          36          45          54          63          72          81          90          10          20          30          40          50          60          70          80          90         100
           1           2           3           4           5           6           7           8           9          10           2           4           6           8          10          12          14          16          18          20           3           6           9          12          15          18          21          24          27          30           4           8          12          16          20          24          28          32          36          40           5          10          15          20          25          30          35          40          45          50           6          12          18          24          30          36          42          48          54          60           7          14          21          28          35          42          49          56          63          70           8          16          24          32          40          48          56          64          72          80           9          18          27          36          45          54          63          72          81          90          10          20          30          40          50          60          70          80          90         100

Can anyone tell me what is going wrong here and how to fix this?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
DerAuenlaender
  • 139
  • 2
  • 6
  • I was finally able to produce a minimal example, you'll find it in the question now. While doing this, I also found that the problem is caused at a different part of the code already, so I rephrased the question. – DerAuenlaender Sep 06 '21 at 11:48
  • 1
    I get the answer you expect with GCC versions 7.5.0, 8.4.0, 9.3.0, 10.3.0 and 11.1.0 – Ian Bush Sep 06 '21 at 12:02
  • Interesting! I just got acces to a machine where I can switch compilers more easily - and: gfortran in version 5.2.0, 7.3.0, 9.1.0, 9.2.0 and 10.2.0 give the expected result, whereas 8.2.0 gives the result as stated in the question. Is this a known bug somehow? – DerAuenlaender Sep 06 '21 at 12:07
  • 1
    I can reproduce your bug with gfortran-8.1.0 from MinGW-w64 (precisely I tested with [this file](https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-win32/seh/)). However, it does not happen if the type and the global variable are defined in a module that is used in both the program and the subroutine. –  Sep 07 '21 at 06:55

0 Answers0