Reputation: 31
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.
Upvotes: 0
Views: 1023
Reputation: 29391
The sequence of lines
ALLOCATE(top%siteTypes(SIZE(tmp)))
tmp(1:SIZE(top%siteTypes)) = top%siteTypes
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)) ..."
Upvotes: 3