- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Using ifx (IFX) 2023.2.0 20230721 (CentOS 8 stream w/ kernel 6.2.1), I compile the following code:
program alextest
implicit none
integer :: my_int_array(100)
integer :: my_integer
integer :: my_indices(8) = (/ 8, 8, 8, 8, 9, 14, 19, 11 /)
my_int_array = 0
do my_integer = 1, 8
print*, my_integer
end do
! adding this line causes the loop to continue
! incrementing 'my_integer' forever:
my_int_array(my_indices(my_integer)) = 99
end program alextest
with the following lines:
ifx -O3 alextest.F90 -o alextest.x
The output consists of infinitely incremented values of 'my_integer' inside the do loop. I can get the desired behavior-- outputting integers 1 through 8--
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The exit value of your do loop leaves the loop control variable (my_integer) with the value of 9.
This value exceeds the size of the array my_indecies, and thus a "junk" value is used to index the array my_int_array.
This is the cause of the segfault.
Note, "not crashing" and "giving the desired behavior" is not the same as working properly.
Also, when the "junk" index produces an address within your process's virtual memory (R/W segment), that you just overwritten something that you should not have overwritten. The fact that the program does not crash (or produces the correct result) is no indication that it is running correctly.
Had you compiled this with full runtime checks enabled, you would have received an error message stating you index an array outside of its bounds.
Jim Dempsey
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The exit value of your do loop leaves the loop control variable (my_integer) with the value of 9.
This value exceeds the size of the array my_indecies, and thus a "junk" value is used to index the array my_int_array.
This is the cause of the segfault.
Note, "not crashing" and "giving the desired behavior" is not the same as working properly.
Also, when the "junk" index produces an address within your process's virtual memory (R/W segment), that you just overwritten something that you should not have overwritten. The fact that the program does not crash (or produces the correct result) is no indication that it is running correctly.
Had you compiled this with full runtime checks enabled, you would have received an error message stating you index an array outside of its bounds.
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Analog to think about
Imagine you are at a gun range. You slip an ammo clip with 8 rounds in it into your pistol, but you forgot about one in the chamber. You take your 8 shots at the target, getting the expected behavior. But when you holster your pistol, you have an accidental discharge.
Now then, if the bullet misses your foot. Is the program running properly?
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Jim, I have learned a valuable lesson about do loops in Fortran-- I had no idea it incremented the index a further time following the loop. Thanks for your help.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page