我正在尝试为'allocate'函数编写一个包装器,即接收数组和维度的函数,分配内存并返回已分配的数组.最重要的是该函数必须使用不同级别的数组.但是我必须在函数接口中明确声明数组的等级,在这种情况下,如果我将某个等级的数组作为参数传递,代码只会编译.例如,此代码无法编译:
module memory_allocator
contains
subroutine memory(array, length)
implicit none
real(8), allocatable, intent(out), dimension(:) :: array
integer, intent(in) :: length
integer :: ierr
print *, "memory: before: ", allocated(array)
allocate(array(length), stat=ierr)
if (ierr /= 0) then
print *, "error allocating memory: ierr=", ierr
end if
print *, "memory: after: ", allocated(array)
end subroutine memory
subroutine freem(array)
implicit none
real(8), allocatable, dimension(:) :: array
print *, "freem: before: ", allocated(array)
deallocate(array)
print *, "freem: after: ", allocated(array)
end subroutine freem
end module …Run Code Online (Sandbox Code Playgroud) fortran ×1