Quantcast
Channel: Intel® Fortran Compiler
Viewing all articles
Browse latest Browse all 3270

Strange behavior of allocatable array with internal procedure

$
0
0

Greetings,

i got a problem with an allocatable array, that gets resized in an subroutine. Without the internal function "testArguments", the array has the expected size of 40 at the end. If "testArguments" is defined (no matter if it is called), the array stays the same size in the main program. "array" seems to become a local variable to "testArguments", instead of a reference to "foo_array".

It it doesn't matter if "testArguments" is a function or subroutine.

It works (any of those points):
- when the array is passed as argument to "testArguments".
- compiler-option /C is used
- optimizations are turned off (/Od)
- "array" is not used in "testArguments"
- "STRANGE_BLOCK" is present

This happens with compilers 15.5 and 16.1 (32bit and 64bit) on windows.

module TEST_MOD
  implicit none
contains

!-------------------------------------------------------------------------------
  subroutine foo
    integer, allocatable :: foo_array(:)

    allocate(foo_array(10))

    call extendArray(foo_array)
    call extendArray(foo_array)
    call extendArray(foo_array)

    ! It works, if this block exists
!    STRANGE_BLOCK : block
!      write(*,*)size(foo_array)
!    end block STRANGE_BLOCK

    write(*,*) 'Expected:'
    write(*,*) 40

    write(*,*) 'Got:'
    write(*,*) size(foo_array, dim=1)

  end subroutine

!-------------------------------------------------------------------------------
  subroutine extendArray(array)
    integer, allocatable, intent(inout) :: array(:)
    integer :: n
  ! ========================

    ! Doesn't matter if called or not.
!    if(.NOT. testArguments()) return

    n = size(array,dim=1)+10
    deallocate(array)
    allocate(array(n))

    write(*,*) size(array,dim=1)

    contains! -------------------------------------------------------------

      ! does not work:
      logical function testArguments() result(valid)
        valid = allocated(array)
      end function testArguments

      ! works: (independent of used arg-name)
!      logical function testArguments(array) result(valid)
!        integer, allocatable, intent(inout) :: array(:)
!        valid = allocated(array)
!      end function testArguments

  end subroutine extendArray

end module TEST_MOD

!===============================================================================

program main
  use TEST_MOD
  call foo()
end program

Wolf


Viewing all articles
Browse latest Browse all 3270

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>