Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!

why ifort cann't check array boundary

mohammad_b_
Beginner
364 Views

Hi 

I use Intel Fortran compiler. my array is 2 dimensions an it's boundary is between 1 to 16.

this is my code and why fortran print a(0,0) and a(17,17) equal to 0 and 0

program hirarchichal_detreministic
          implicit none
          
          integer, parameter:: N=4 !initial core
          integer, parameter:: ns=1 !number of steps
          integer, parameter:: nn = n**(ns+1)
          integer, dimension (1:nn,1:nn):: a !adjacency matrix
          
          integer:: i, j, k, ii, m, l
          
          real::p(nn)
          
          open(1,file='out')
          
          a = 0
          print*,sum(a), a(0,0), a(17,17)
          pause
          
          !******************************************
          !initial fully connected network; ns=0
          a(1:n,1:n)=1
          
          do i=1,n
			 a(i,i)=0
          enddo
          !******************************************
          print*,sum(a), a(0,0), a(17,170)
          do i=1,ns
          
			 do k= 2, n-1
				    
				    do ii= k*(n**i)+1, (k+1)*(n**i)
						  
						  do j=k*(n**i)+1, (k+1)*(n**i)
								!print*,i,k,ii,j
								l = mod(ii,n**i+1)
								m = mod(j,n**i+1)
								a(ii,j) = a(l,m)
								print '(20(I2,2x))',sum(a), i, k, ii, j, l, m
						  enddo
						  
						 ! pause
				    enddo
			 enddo
          enddo
          print*,"fd"
		  Do i=1, nn
			 !k=sum(a(1:nn,i))
			 !p(k)=p(k)+1.
			 do j=1,nn
				 write(1,*)i,j, a(i,j)
			 enddo
		  enddo
		  
		  p=p/sum(a)
		   print*,"fd/"
		  do i=1,nn
			 write(1,*)i,p(i)
		  enddo
          end
          

this is my code and why fortran print a(0,0) and a(17,17)

0 Kudos
1 Solution
jimdempseyatthecove
Black Belt
364 Views

A segmentation fault occurs when the resolved address resides within a virtual memory page that has not been assigned (an marked as not assignable). For a valid array, indexing 1 or a few element before or after the addresses of the array typically resolve to mapped addresses (which in Fortran-speak are undefined). Using bounds checking in most cases will catch these errors. In some cases where the array is a dummy argument, the dummy argument can potentially be specified with incorrect bounds with respect to the actual argument. In these cases the bounds checking asserts the bounds specified for the dummy argument as opposed to the actual argument at the call.

Jim Dempsey

View solution in original post

4 Replies
Laura_S_3
Beginner
364 Views

You need to compile with the compiler options set properly. For example,

ifort -check all drb.f90 <other options that you need>

will add the code to check the bounds at run-time.

mohammad_b_
Beginner
364 Views

why not a segmentation fault error?

jimdempseyatthecove
Black Belt
365 Views

A segmentation fault occurs when the resolved address resides within a virtual memory page that has not been assigned (an marked as not assignable). For a valid array, indexing 1 or a few element before or after the addresses of the array typically resolve to mapped addresses (which in Fortran-speak are undefined). Using bounds checking in most cases will catch these errors. In some cases where the array is a dummy argument, the dummy argument can potentially be specified with incorrect bounds with respect to the actual argument. In these cases the bounds checking asserts the bounds specified for the dummy argument as opposed to the actual argument at the call.

Jim Dempsey

View solution in original post

mecej4
Black Belt
364 Views

mohammad bahrami wrote:

why not a segmentation fault error?

Because a(17,17) simply refers to the same address in memory as p(11), assuming no padding and, therefore, sequential allocation of a(,), followed by the six integers, and then p. Thus, the address of a(17,17) is still within the memory block allocated for use by your program.

More simply, given the declaration

Integer a(2), b(3)

a run-time reference to a(4) is the same as a reference to b(2). This is why you need a finer check on individual variables rather than the whole block of memory that the program "owns".

Reply