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

In summary, you are having a segmentation fault error just when you ask the code to print the value ipos in the program. However, if you 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.
  • #1
yomar
2
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
  • #2


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.
 
  • #3


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.
 
  • #4


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.
 
  • #5


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
 

1. What is a segmentation fault?

A segmentation fault, also known as a segfault, is a type of error that occurs when a program attempts to access a memory address that is not assigned to it. This can happen due to a programming mistake or an attempt to access an invalid or inaccessible memory location.

2. How do I fix a segmentation fault in Fortran?

There are several steps you can take to try and fix a segmentation fault in Fortran. First, check your code for any logical errors, such as using uninitialized variables or accessing arrays out of bounds. You can also use a debugger to track down the specific line of code that is causing the error. Additionally, make sure you are using the correct data types and memory allocation methods.

3. Why am I getting a segmentation fault when my code used to work?

A segmentation fault can occur for various reasons, such as changes in the code, compiler updates, or changes in the system environment. It could also be caused by a memory leak or stack overflow. Make sure to carefully review any recent changes to your code and consider potential environmental factors.

4. Can a segmentation fault cause data loss?

Yes, a segmentation fault can cause data loss if the error occurs while writing to a file or manipulating data in memory. It is essential to regularly save your work while coding and handle potential errors to avoid data loss.

5. How can I prevent segmentation faults in my Fortran code?

To prevent segmentation faults, it is crucial to write robust and error-free code. Make sure to properly initialize variables, use appropriate data types, and avoid accessing memory locations that are not assigned to your program. Utilizing debugging tools and testing your code thoroughly can also help identify and prevent potential errors.

Similar threads

  • Programming and Computer Science
Replies
4
Views
498
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
5
Views
12K
  • Programming and Computer Science
Replies
5
Views
24K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
5
Views
7K
Back
Top