Fortran [FORTRAN] Assigning bad values in arrays

  • Thread starter Thread starter Matterwave
  • Start date Start date
  • Tags Tags
    Arrays Fortran
AI Thread Summary
The discussion centers on issues with array assignments in Fortran, specifically the consequences of assigning values to out-of-bounds indices. The user initially assigns values from one array to another incorrectly, leading to potential garbage values in the second array without triggering compile-time or run-time errors. It is noted that Fortran does not perform bounds checking by default to avoid performance slowdowns, but this can be enabled with the -fbounds-check compiler option during debugging. The conversation highlights the importance of implementing safeguards against out-of-bounds errors and the limitations of compile-time checks for dynamic indices. Overall, the need for better error detection in Fortran is emphasized, particularly for common programming mistakes.
Matterwave
Science Advisor
Homework Helper
Gold Member
Messages
3,971
Reaction score
329
Hi, I just realized that I have been making bad array assignments in my code. Specifically, I think I have been assigning to slots that the array does not have. For example, I might declare an array:

Code:
int,parameter :: nflavor=3
int,parameter :: nflavor2=nflavor*2
real,dimension(nflavor)::x
real,dimension(nflavor2)::y

And then assign values of y from values of x in such a fashion:

Code:
do i=1,nflavor
y(i)=x(i)
end do

do i=nflavor+1,nflavor2
y(i)=x(i)
end do

So for the second do-loop, I should be going over values of i for slots that x doesn't have, right?
I realized that what I should have written was something like:
Code:
do i=nflavor+1,nflavor2
y(i)=x(i-nflavor)
end do

Now that I think about it, I would have guessed I should have incurred a bunch of compile-time or at least run-time errors. However, with these errors, the code actually still ran. I did not get compile or run-time errors using these faulty array assignments. Can anyone guess what Fortran has been doing?
 
Technology news on Phys.org
What values are in y(4), y(5), and y(6)? I'm guessing they are garbage values; i.e., whatever happens to be in memory following x(3).
 
Ah I see. No wonder I was getting some bogus values...I would have hoped to get a compiler error or at least a warning of some sort...but I guess not? What if I had written it the other way around though, like:

Code:
do i=nflavor+1,nflavor2
x(i)=y(i)
end do

Now is it assigning random memory addresses with values from y(i)?
 
Matterwave said:
Ah I see. No wonder I was getting some bogus values...I would have hoped to get a compiler error or at least a warning of some sort...but I guess not? What if I had written it the other way around though, like:

Code:
do i=nflavor+1,nflavor2
x(i)=y(i)
end do

Now is it assigning random memory addresses with values from y(i)?
Yes, which is not a good thing to do.
 
Mark44 said:
Yes, which is not a good thing to do.

It seems like...there should be a compiler warning for issues such as this. I hope I haven't made this error in more places than I can remember! D: I've fixed it everywhere I can remember making such assignments.
 
I did a quick check of the the GNU Fortran compiler docs, and I didn't see any warnings that were specific to indexing an array beyond its bounds.
 
Mark44 said:
I did a quick check of the the GNU Fortran compiler docs, and I didn't see any warnings that were specific to indexing an array beyond its bounds.

Haha, perhaps they should implement that...seems quite dangerous! And it's a pretty easy error to make if you ask me.
 
When you compile the program, you can tell gfortran to generate runtime code that checks for out-of-bounds array indexes, by using the -fbounds-check compiler option. See

http://linux.die.net/man/1/gfortran

(down towards the end of the page)

Fortran programs don't do this by default because the extra code makes the program run slower. Normally you use this only when testing and debugging. When you're satisfied that the program is running correctly, with all arrays staying within bounds, you can re-compile with bounds checking turned off, if speed is important.
 
jtbell said:
When you compile the program, you can tell gfortran to generate runtime code that checks for out-of-bounds array indexes, by using the -fbounds-check compiler option. See

http://linux.die.net/man/1/gfortran

(down towards the end of the page)

Fortran programs don't do this by default because the extra code makes the program run slower. Normally you use this only when testing and debugging. When you're satisfied that the program is running correctly, with all arrays staying within bounds, you can re-compile with bounds checking turned off, if speed is important.


Ah, I thought that the bounds checking would have been really simple, once over look, like a syntax error or something. I guess it's not always so easy? Thanks for the info! :)
 
  • #10
Bounds checking has to be done at run time not compilation time, because array indices are usually variables. The compiler often can't easily figure out what values a variable will contain when you run the program. Sometimes it's impossible, e.g.:

Code:
      real frammis(10)
      integer n
      print *, 'Which frammis do you want? '
      read *, n
      print *, 'OK, here it is: ', frammis(n)
 
  • #11
jtbell said:
Bounds checking has to be done at run time not compilation time, because array indices are usually variables. The compiler often can't easily figure out what values a variable will contain when you run the program. Sometimes it's impossible, e.g.:

Code:
      real frammis(10)
      integer n
      print *, 'Which frammis do you want? '
      read *, n
      print *, 'OK, here it is: ', frammis(n)

I see. But this requires some kind of input external to the program, in which case it would be good for the programmer to put some fail safes e.g.

Code:
      real frammis(10)
      integer n
      print *, 'Which frammis do you want? (integer from 1 through 10)'
      read *, n
      if (n .ge. 1 .and. n .le. 10)
      print *, 'OK, here it is: ', frammis(n)
      else
      print *, 'Please enter a valid integer from 1 through 10'
      read *, n
      end if

It seems though that Fortran should be able to easily check at compile time any instances of bad array bounds where the arrays are both not allocatable and its array assignments in the code are all within the code itself and not read through the console or through a file. Such as the instances in my code where I had loops go outside of array bounds.
 
  • #12
That is somewhat over the top for a compiler. Also, any code worth it's salt and written to actually do something useful would not have many cases where array bound errors could be spotted at compile time
 
  • #13
a_potato said:
That is somewhat over the top for a compiler. Also, any code worth it's salt and written to actually do something useful would not have many cases where array bound errors could be spotted at compile time

Thanks for insulting my code. :s:L
 
  • #14
A good compiler should be able to figure out what the programmer really wants to do, instead of just doing what the programmer has programmed.
 

Similar threads

Replies
6
Views
2K
Replies
21
Views
2K
Replies
20
Views
3K
Replies
4
Views
2K
Replies
4
Views
2K
Replies
3
Views
2K
Back
Top