使用 ALLOCATE 和 SegFault 错误
我正在使用 gfortran 编译器 (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 编译一些 FORTRAN 90 代码
编译代码后,当我尝试运行该程序时,出现分段错误错误。使用 Valgrind 后,我能够在下面的代码部分中找到问题。可以看出,使用了 DEALLOCATE,而没有使用前面的 ALLOCATE。我应该注意,我不是编写该软件的人,并且它是使用 f90 成功编译的,但是我无法再访问编写/编译该软件的原始计算机。
CONTAINS
!! NewSiteType --------------------------------------------------------------------------
---
!!
!!- Searches for the index of a particular atom label
!!
FUNCTION NewSiteType(top,name,mass)
IMPLICIT NONE
!-------- function parameters ----------------
INTEGER :: NewSiteType
TYPE(TTopology), INTENT(INOUT) :: top
CHARACTER(*), INTENT(IN) :: name
REAL*8, INTENT(IN) :: mass
!-------- local variables --------------------
TYPE(TSiteType), DIMENSION(SIZE(top%siteTypes)+1) :: tmp
INTEGER :: i
NewSiteType = 0
! check if a site type was already registered
DO i = 1,SIZE(top%siteTypes)
IF (top%siteTypes(i)%name == name) THEN
NewSiteType = i
RETURN
END IF
END DO
! no, enlarge the list by the current one
! adding in an ALLOCATE to address a segfault problem hopefully
ALLOCATE(top%siteTypes(SIZE(tmp)))
tmp(1:SIZE(top%siteTypes)) = top%siteTypes
DEALLOCATE(top%siteTypes)
ALLOCATE(top%siteTypes(SIZE(tmp)))
top%siteTypes = tmp
top%siteTypes(SIZE(top%siteTypes)) = TSiteType(name,mass)
NewSiteType = SIZE(top%siteTypes)
END FUNCTION NewSiteType
为了解决这个问题,我添加了以下行
ALLOCATE(top%siteTypes(SIZE(tmp)))
这解决了段错误问题,但是现在这个功能无法正常工作。它的目的是获取输入并搜索数组,如果输入不在数组中则添加它。但是,现在我已经添加了初始 ALLOCATE,它似乎没有按预期将未注册的输入添加到数组中。我认为这是因为当程序尝试使用尚未注册的网站时会生成一个特定错误。由于这是我所做的唯一更改,我猜测我只是通过添加 ALLOCATE 做了一些错误的事情。
我确实想提一件事。当我最初运行 Valgrind 来发现内存问题时,它实际上似乎按预期运行了整个可执行文件?这对我来说似乎很奇怪。
I am compiling some FORTRAN 90 code using gfortran compiler (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
After compiling my code I was getting a Segmentation Fault error when I would attempt to run the program. After using Valgrind I was able to locate the problem in the below section of code. As can be seen a DEALLOCATE was being used without a preceding ALLOCATE. I should note that I am not the one who wrote this software and it was successfully compiled using f90 however I no longer have access to the original machine this was written/compiled on.
CONTAINS
!! NewSiteType --------------------------------------------------------------------------
---
!!
!!- Searches for the index of a particular atom label
!!
FUNCTION NewSiteType(top,name,mass)
IMPLICIT NONE
!-------- function parameters ----------------
INTEGER :: NewSiteType
TYPE(TTopology), INTENT(INOUT) :: top
CHARACTER(*), INTENT(IN) :: name
REAL*8, INTENT(IN) :: mass
!-------- local variables --------------------
TYPE(TSiteType), DIMENSION(SIZE(top%siteTypes)+1) :: tmp
INTEGER :: i
NewSiteType = 0
! check if a site type was already registered
DO i = 1,SIZE(top%siteTypes)
IF (top%siteTypes(i)%name == name) THEN
NewSiteType = i
RETURN
END IF
END DO
! no, enlarge the list by the current one
! adding in an ALLOCATE to address a segfault problem hopefully
ALLOCATE(top%siteTypes(SIZE(tmp)))
tmp(1:SIZE(top%siteTypes)) = top%siteTypes
DEALLOCATE(top%siteTypes)
ALLOCATE(top%siteTypes(SIZE(tmp)))
top%siteTypes = tmp
top%siteTypes(SIZE(top%siteTypes)) = TSiteType(name,mass)
NewSiteType = SIZE(top%siteTypes)
END FUNCTION NewSiteType
To fix the problem I added the lines
ALLOCATE(top%siteTypes(SIZE(tmp)))
This fixed the Segfault problem however now this function is not working properly. It is intended to take input and search an array and if the input is not in the array add it. However, now that I have added the initial ALLOCATE it appears to not be adding unregistered input to the array as it is supposed to. I think this because there is a specific error that is generated when the program is attempting to use sites that have not been registered. And since this is the only change I have made I am guessing I am just doing something incorrectly by adding the ALLOCATE.
I do want to mention one thing. When I run Valgrind initially to find the problem with the Memory it actually appears to run the whole executable as it should? That seems strange to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
行的顺序
没有意义。第一个为 top%siteTypes 保留内存,第二个通过将变量放在赋值的 RHS 上来使用该内存的内容,但该变量尚未初始化。将分配语句放在您所在的位置并不能解决变量由于上面使用而未分配的问题。
问题可能出在该函数之外,该函数显然假设 top%siteTypes 已分配并初始化。 “inout”的意图表明变量 top 旨在作为输入(和输出),而在条目上分配将擦除变量的内容,因此它实际上变成仅输出。您可以通过“if (.not. allocate (top%siteTypes)) ...”检查是否未分配
The sequence of lines
does not make sense. The first reserves memory for top%siteTypes and the second uses the contents of that memory via having the variable on the RHS of an assignment, but the variable has not been initialized. Locating the allocate statement where you have it would not fix the problem of the variable not being allocated since it used above.
The problem may be outside of this function, which apparently assumes that top%siteTypes is allocated and initialized. The intent of "inout" suggests that variable top is intended to be input (and output), while allocating upon entry will erase the content of the variable so it becomes effectively only output. You can check for not being allocated with by "if (.not. allocated (top%siteTypes)) ..."