How can I change the starting index of an array in Fortran?

In summary, the code provided is attempting to read files in subdirectories and create new files in the current location. However, a segmentation fault occurs and debugging reveals that the value of j changes from 1 to a random number after defining fileout(i)=mass//'_000.dat'. This can be solved by declaring the array with 0 as the smallest index, but it is important to note that arrays in Fortran are typically 1-based and it is recommended to get used to working with this convention.
  • #1
Giammy85
19
0
I'm try to read some files in subdirectories and create some other files in my present location:

program multiread
!
implicit none

!variables
integer :: n, k, i
integer :: j, m2
real(8) :: pratio, crap, rmsttv
character(len=42) :: filein(98)
character(len=10) :: fileout(14)
character (LEN=26) :: path
character(len=2) number, mass

j = 1
do i=0, 6
m2=5*i
if (m2==0) then
m2=1
end if
write(mass,998) m2
998 format(i2.2)
write(*,*) j
fileout(i)=mass//'_000.dat'
write(*,*) fileout(i),j
open(unit=i+10,file=fileout(i),status='replace',access="append")
! write(*,*) j
do n=0,6
pratio=1.5+0.25*n
! write(*,*) pratio
write(number,998) j
! write(*,*) j
! 999 format(i2.2)
write(*,*) number
path='/scratch/project3/set1/'//number//'/'
filein(j)=path//'periodrmsttv.dat'
write(*,*) filein(j)
open(unit=j+40,file=filein(j),status='old')

read (j+40,*) crap
read (j+40, '(f14.6)') rmsttv
write (i+10,'(f4.2,f14.6)') pratio, rmsttv
close (j+40)

j=j+2
end do
close (i+10)
end do

end program multiread

I receive a segmentation fault message: debugging I have found the value of j change from 1 to a random number just after having defined fileout(i)=mass//'_000.dat'
How to solve this problem?
Thanks
 
Technology news on Phys.org
  • #2
When you post a code sample, put [ code ] and [ /code ] tags around it (omit the spaces I showed). This preserves your indentation and makes your code easier to read. I have done that below.
Giammy85 said:
I'm try to read some files in subdirectories and create some other files in my present location:
Code:
program multiread
!
implicit none 

!variables
integer :: n, k, i
integer :: j, m2
real(8) :: pratio, crap, rmsttv
character(len=42) :: filein(98)
character(len=10) :: fileout(14)
character (LEN=26) :: path
character(len=2) number, mass

j = 1
do i=0, 6
m2=5*i
if (m2==0) then
m2=1
end if
	   write(mass,998) m2
	998  format(i2.2)
      write(*,*) j
	   fileout(i)=mass//'_000.dat'
      write(*,*) fileout(i),j
	   open(unit=i+10,file=fileout(i),status='replace',access="append")
 !     write(*,*) j
	do n=0,6	
	pratio=1.5+0.25*n
!      write(*,*) pratio
	      write(number,998) j
!      write(*,*) j
!	 999  format(i2.2)
      write(*,*) number
		path='/scratch/project3/set1/'//number//'/'
	      filein(j)=path//'periodrmsttv.dat'
      write(*,*) filein(j)
	      open(unit=j+40,file=filein(j),status='old')

		read (j+40,*) crap
		read (j+40, '(f14.6)') rmsttv
		write (i+10,'(f4.2,f14.6)') pratio, rmsttv
		close (j+40)

	j=j+2
	end do
close (i+10)
end do

end program multiread
I receive a segmentation fault message: debugging I have found the value of j change from 1 to a random number just after having defined fileout(i)=mass//'_000.dat'
How to solve this problem?
Thanks

I believe this is your problem:
Code:
do i=0, 6
  m2=5*i
  if (m2==0) then
  m2=1
  end if
  write(mass,998) m2
  998 format(i2.2)
  write(*,*) j
  fileout(i)=mass//'_000.dat'
  .
  .
  .
end do
Arrays in Fortran are 1-based, by default, which means that the smallest index is 1. In your code, i starts out at 0, and you attempt to store the value of mass in fileout(0).
 
Last edited:
  • #3
Mark44 said:
Arrays in Fortran are 1-based, which means that the smallest index is 1. In your code, i starts out at 0, and you attempt to store the value of mass in fileout(0).

You can declare a Fortran array with any integer bounds if you want, for eaxmple
Code:
character(len=10) :: fileout(0:14)
makes the smallest index 0 not 1.

But if you are going to use Fortran a lot, you need to get used to working with arrays that start from index 1.
 
  • #4
Thanks, solved!
 
  • #5
AlephZero said:
You can declare a Fortran array with any integer bounds if you want, for eaxmple
Code:
character(len=10) :: fileout(0:14)
makes the smallest index 0 not 1.
I thought there would be a way to do it. What I meant to say was that by default, an array is 1-based.
AlephZero said:
But if you are going to use Fortran a lot, you need to get used to working with arrays that start from index 1.
 

Related to How can I change the starting index of an array in Fortran?

1. What is a segmentation fault in Fortran90?

A segmentation fault in Fortran90 is an error that occurs when a program tries to access memory that it does not have permission to access. This can happen due to a variety of reasons, such as attempting to access an array element that is out of bounds or trying to access memory that has already been deallocated.

2. How does a segmentation fault happen in Fortran90?

A segmentation fault in Fortran90 can happen due to a variety of reasons, including attempting to access memory that is out of bounds, trying to access memory that has already been deallocated, or passing incorrect arguments to a function or subroutine.

3. How can I debug a segmentation fault in Fortran90?

To debug a segmentation fault in Fortran90, you can use a debugger such as GDB or Intel Inspector. These tools will help you identify the line of code that is causing the error and provide information about the state of your program's memory at the time of the error.

4. Can a segmentation fault in Fortran90 be prevented?

Yes, a segmentation fault in Fortran90 can be prevented by writing code that is free of memory access errors and by using proper error checking and handling techniques. Additionally, using a debugger and performing thorough testing can help identify and fix potential segmentation faults before they occur.

5. Are there any common mistakes that can lead to a segmentation fault in Fortran90?

Yes, there are several common mistakes that can lead to a segmentation fault in Fortran90, such as not properly declaring and initializing variables, accessing memory that is out of bounds or has already been deallocated, and passing incorrect arguments to functions or subroutines. It is important to carefully review your code and use proper error checking techniques to avoid these mistakes.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
8
Views
4K
Back
Top