Pergunta

I have to deal with some legacy Fortran code. As far as I prefer to use ALLOCATE to deal with arrays, it was a surprise to see things like

      INTEGER*4 A(1)
      ... 
      DO 90 J=1,N
      A(J) = SomeValues(J)
90    CONTINUE

The problem is silent memory corruption with such arrays. Right after one of them gets filled with values, it becomes corrupted for some unknown reason.

      NEQNS=0
      A(1)=1
      DO 100 I=1,NFULL
      IND=A(I)-1
      S=0
      DO 90 J=1,6
      IF(MASK(I,J)) THEN
      S=S+1
      NEQNS=NEQNS+1
      B(NEQNS)=I
      ENDIF
 90   CONTINUE
      A(I+1)=A(I)+S
100   CONTINUE

c Another array starts here to be used in next cycle
      C(1)=1
      DO 700 IL=1,NEQNS
      I=B(IL)  !  Value of B(1) is broken here!
      ....

Debugging is not so easy because I have watches like this: enter image description here

or like this enter image description here

The program was built years ago and it's working, but rebuilt versions are broken. I used x64 and x86 machines, same result.

So, what should I do to fix it, any ideas? Changing allocation method is the least preferred option, as there is too much code to deal with.

Foi útil?

Solução

This is old FORTRAN where memory had to be allocated statically, at compile time. Frequently programmers just passed the starting address of an array to subroutines, as array(1). The caller had to have reserved sufficient memory with a declaration. Your example will only work if this code block came from a procedure for which A is a dummy argument. And the calling routine or higher declared A large enough such that N and NFULL+1 are legal elements.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top