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

Click For Summary

Discussion Overview

The discussion revolves around a segmentation fault error encountered in a Fortran program when attempting to print the value of the variable ipos. Participants explore issues related to variable allocation and scope, particularly in the context of modules and subroutines.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports a segmentation fault occurring when printing ipos in the main program, suggesting a potential issue with transferring the variable from the module.
  • Another participant suggests that the variable ipos has not been allocated properly, despite being declared as allocatable.
  • A different participant notes that removing the allocate and deallocate statements still results in a segmentation fault, indicating that the issue persists regardless of those statements.
  • One participant points out the existence of two separate ipos variables, one in the main program and another in the module, suggesting a namespace problem that could be causing the segmentation fault.
  • Another participant advises moving the declaration of ipos to the module outside of the subroutine to resolve potential scope issues.

Areas of Agreement / Disagreement

Participants express differing views on the cause of the segmentation fault, with some attributing it to allocation issues and others to variable scope. No consensus is reached on the solution to the problem.

Contextual Notes

The discussion highlights limitations related to variable scope and allocation in Fortran, with unresolved questions about the correct usage of allocatable variables and their visibility across modules and subroutines.

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
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
13K
  • · Replies 5 ·
Replies
5
Views
25K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
8K