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

  • Context: Fortran 
  • Thread starter Thread starter Giammy85
  • Start date Start date
  • Tags Tags
    Fault Fortran90
Click For Summary

Discussion Overview

The discussion revolves around changing the starting index of an array in Fortran, specifically addressing a segmentation fault encountered in a code snippet. Participants explore the implications of Fortran's default 1-based indexing and how to declare arrays with different bounds.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a Fortran program that encounters a segmentation fault, noting that the variable j changes unexpectedly after defining an array element.
  • Another participant suggests that the issue arises from using a 0-based index for the array fileout, which is not compatible with Fortran's default 1-based indexing.
  • Some participants propose that declaring the array with explicit bounds (e.g., character(len=10) :: fileout(0:14)) allows for a 0-based index.
  • There is a reminder that while changing array bounds is possible, it is important to adapt to Fortran's default indexing conventions when programming in the language.

Areas of Agreement / Disagreement

Participants generally agree on the default behavior of Fortran arrays being 1-based and the potential to declare arrays with different bounds. However, there is no consensus on the best practices for managing array indices in Fortran programming.

Contextual Notes

The discussion highlights the importance of understanding array indexing in Fortran, particularly for those new to the language. There are no resolved mathematical steps or assumptions explicitly stated.

Who May Find This Useful

This discussion may be useful for programmers working with Fortran, particularly those encountering issues related to array indexing and segmentation faults.

Giammy85
Messages
19
Reaction score
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
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[/color], 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:
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.
 
Thanks, solved!
 
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.
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K
Replies
55
Views
7K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 8 ·
Replies
8
Views
5K