可分配的数组已经分配

可分配的数组已经分配

本文介绍了错误的fortran错误:可分配的数组已经分配;DEALLOCATE指向无法取消分配的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个fortran代码,但该代码以我不理解的方式失败了.我试图在下面清楚地解释这种情况,但请让我澄清一下是否不清楚.

I have written a fortran code which is failing in a way that I do not understand. I have tried to explain the scenario clearly below but please ask me to clarify if it is not clear.

代码包含一个子例程

SUBROUTINE TMGP(NSYM,NOB,NFT,DEN,ncod,PR,np,lden,NPMX,nuccen,mdel)

IMPLICIT NONE
...
INTEGER :: NINTS
REAL(KIND=wp), ALLOCATABLE :: XBUF(:)
...

print *,"xbuf allocated ?",allocated(xbuf)
print *,"xbuf not allocated ?",.not.allocated(xbuf)

if (allocated(xbuf)) then
    DEALLOCATE(xbuf)
end if

if (.not.allocated(xbuf)) then
    allocate (xbuf(nints))
end if

...

RETURN

END SUBROUTINE TMGP

但是,当我运行该程序时,它在行上失败:

However when I run this program it fails on the line:

DEALLOCATE(xbuf)

有错误

forrtl: severe (173): A pointer passed to DEALLOCATE points to an array that cannot be deallocated

输出:

xbuf allocated ? T
xbuf not allocated ? F

如果我将代码更改为(除了添加此行,没有其他更改):

If I change the code to (and make NO other changes than adding this line):

print *,"xbuf allocated ?",allocated(xbuf)
print *,"xbuf not allocated ?",.not.allocated(xbuf)
print *,"nints = ",nints

if (allocated(xbuf)) then
    DEALLOCATE(xbuf)
end if

if (.not.allocated(xbuf)) then
    allocate (xbuf(nints))
end if

并运行此程序,它在以下行失败:

and run this program it fails on the line:

allocate (xbuf(nints))

然后我收到消息:

forrtl: severe (151): allocatable array is already allocated

输出:

xbuf allocated ? F
xbuf not allocated ? T
nints =                      1

对此我感到很困惑,因为在我看来,我使用的if语句应该使这种问题变得不可能,并且按照代码的逻辑,这些错误也不应该发生.

I am quite baffled by this as it seems to me the if statements I have used should make this kind of issue impossible and following the logic of the code these errors should not be possible.

我正在使用ifort编译器,对于可能导致此问题的任何想法或有关解决方法的建议,将不胜感激.

I am using ifort compiler, any ideas on what could be causing this issue or suggestions on how to fix it would be very much appreciated.

请让我知道是否还有其他有用的信息.

Please let me know if any additional information would be useful.

詹姆斯

推荐答案

您的观察结果很可能是内部描述符损坏的结果,编译器用来跟踪可分配(或指针)对象的状态并与该方式交互特定的编译器在其中存储和测试对象的分配状态.

Your observations are a likely consequence of corruption of the internal descriptor that the compiler uses to track the status of an allocatable (or pointer) object, interacting with the way in which that specific compiler stores and tests the allocation status of an object.

这是该特定编译器的很多实现细节,但是至少有两种方法可以将对象的已分配"状态存储在该对象的描述符中-实际数据的非空机器地址支持对象,并在描述符中设置一个单独的标志.分配的内部函数正在查看这些信息之一,而对ALLOCATE语句的运行时支持正在查看另一条信息.通常,这两条信息是一致的,但是面对内存损坏,您会看到明显的矛盾.可以在 https:/中找到有关Intel Fortran当前版本的描述符的布局的文档./software.intel.com/zh-CN/node/525356 .

This is very much implementation detail for that specific compiler, but there are at least two ways in which the "allocated" status of an object is stored in the descriptor for the object - a non-null machine address for the actual data backing the object, and a separate flag set in the descriptor. The allocated intrinsic is looking at one of these pieces of information, while the runtime support for the ALLOCATE statement is looking at another. Normally these two pieces of information are consistent, but in the face of memory corruption you see the apparent contradiction. Documentation for the layout of the descriptor for the current version of Intel Fortran can be found at https://software.intel.com/en-us/node/525356.

(另一个标志跟踪描述符是否用于可以释放的对象(即,它是由ALLOCATE或类似方法创建的)或(可能是描述符用于引用另一个数组的一部分的指针)-损坏标志会导致显示"...无法释放数组"消息.)

(Another flag tracks whether the descriptor is for something that can be deallocated (i.e. it was something created by ALLOCATE or similar) or not (perhaps the descriptor is for a pointer that references a section of another array) - corruption of that flag results in the "... array cannot be deallocated" message.)

内存损坏很可能是由程序错误引起的,该错误可能不在数组声明和使用的直接源位置.正如其他人在注释中所建议的那样-在代码中寻找编程错误,例如数组下标超出范围或参数列表不匹配.编译器的运行时诊断选项通常会有所帮助.具有适当的调试能力,您还可以监视相关数组描述符的内存存储,并确切确定何时首次出现描述符状态不一致的情况.

The memory corruption is likely caused by an error in your program, which may not be in the immediate source location of the array declaration and use. As suggested by others in the comments - go looking for programming errors in your code such as array subscripts being out of bounds or mismatches in argument lists. The compiler's runtime diagnostic options often help. With appropriate debugging ability, you can also monitor the memory storage for the relevant array descriptor, and determine exactly when the inconsistency in the state of the descriptor first appears.

这篇关于错误的fortran错误:可分配的数组已经分配;DEALLOCATE指向无法取消分配的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:32