[Fortrran] Some help with a code error: Segmentation Fault

AI Thread Summary
The discussion centers on a segmentation fault error encountered when printing the variable `ipos` in a Fortran program. The user experiences this error despite successfully printing values from within a module. Key points include the need for proper allocation of the `ipos` variable, as it is declared as allocatable but not correctly allocated in the main program. A suggestion is made to eliminate the local declaration of `ipos` within the subroutine and instead declare it in the module, allowing it to act as a global variable accessible in both the main program and the subroutine. This approach would resolve the namespace conflict and ensure that `ipos` is properly allocated before use, thus eliminating the segmentation fault.
yomar
Messages
2
Reaction score
0
Hi,
I am having a segmentation fault error just when I ask the code to print the value ipos in the program. However, If I change the print to the module, the segmentation fault does not appear. Could anyone can help know, why I can not transfer the variable ipos from the module to the main program or how eliminate that segmentation fault once I request print the variable in the program? Thanks.

!---------------------------------------------------
Program pro
Use Random
Implicit None

real*8 , Allocatable , Dimension (:) :: ipos
integer :: i , m
Real*8 :: u , t

m = 20
t = 1

do i = 1 , m
call Particle_alloc (ipos, m)
Print*, ipos (i)
end do

End Program pro

!----------------------------------------------------




Module Random

Implicit None
CONTAINS

!---------------------------------------------------------------------------------------------------
Subroutine Particle_alloc (ipos , m)

Implicit None

integer*4 :: m , j
real*8, Allocatable, dimension(:) :: ipos

Allocate (ipos (m))


do j = 1 , m

call init_random_seed
! Generate random number for position
call random_number(ipos)

end do

deallocate (ipos)
End Subroutine Particle_alloc
!---------------------------------------------------------------------------------------------------


Subroutine init_random_seed
!-----------------------------------------------------------------------
! Subroutine obtained from: http://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html
!-----------------------------------------------------------------------
integer :: i, n, clock

integer, dimension(:), allocatable :: rseed
call random_seed(size = n)

allocate( rseed(n) )

call system_clock(count=clock)
rseed = clock + 37 * (/ (i - 1, i = 1, n) /)
call random_seed(PUT = rseed)

deallocate(rseed)
End Subroutine
!-----------------------------------------------------------------------------------------------------


End Module Random
 
Technology news on Phys.org


Hey yomar and welcome to the forums.

It looks like you actually haven't allocated the variable, but just defined the type and declared to be allocatable without actually allocating it.
 


Hi Chiro, thanks for you advises, even so
If I eliminate the allocate and deallocate statement, I also obtain the segmentation fault error, but without any result. At least, as is show here, it gave the ipos values from the print statement on the module. However, I still not find the way to made it from the main program.
 


yomar said:
Hi Chiro, thanks for you advises, even so
If I eliminate the allocate and deallocate statement, I also obtain the segmentation fault error, but without any result. At least, as is show here, it gave the ipos values from the print statement on the module. However, I still not find the way to made it from the main program.

No that's not what I mean: you have to actually use the ALLOCATE statement in fortran: Example:

Code:
 ALLOCATE(ipos(N))

for some value of N.
 


I see 2 separate ipos variables declared, one in the main program and another one in the module inside the subroutine...so, I think you have some kind of namespace problem.

Stop declaring the one in the main program
Move the declaration to the module, but not inside the subroutine; place the declaration between "implicit none" and "contains"
When a variable is declared in a module is like a global variable as such, you will be able to refer to it in the main program. Also, you will be able to allocate it within the subroutine, but also, you need to eliminate the declaration of the local ipos inside the sub
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...
Back
Top