数组索引超出范围 [英] Index of array out of range

查看:152
本文介绍了数组索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用于引用数组元素的索引实际上超出其预期范围,Fortran如何处理这种情况,我感到困惑。

I am confused how Fortran handle the situation if the index for referencing an element of an array is actually out of its supposed range.

以下是说明问题的简单代码:

Here is a simple code to illustrate the problem:

PROGRAM test_matrix_out

USE mod_writearray

IMPLICIT NONE
INTEGER :: i,j,m,n
REAL    :: k
REAL, Dimension(:,:),ALLOCATABLE :: A

m = 3
n = 4
ALLOCATE(A(m,n))

k = 1

DO i=1,m
    DO j=1,n
        A(i,j)=k
        k=k+1
    ENDDO
ENDDO

CALL writearray(A)
WRITE(*,*)
WRITE(*,*) A(1,:)
WRITE(*,*)
WRITE(*,*) A(2,:)
WRITE(*,*)
WRITE(*,*) A(0,:)
WRITE(*,*)
WRITE(*,*) A(4,:)
WRITE(*,*)
WRITE(*,*) A(5,:)
WRITE(*,*)
WRITE(*,*) A(100,:)
WRITE(*,*)
WRITE(*,*) A(:,1)
WRITE(*,*)
WRITE(*,*) A(:,2)
WRITE(*,*)
WRITE(*,*) A(:,0)
WRITE(*,*)
WRITE(*,*) A(:,4)
WRITE(*,*)
WRITE(*,*) A(:,5)
WRITE(*,*)
WRITE(*,*) A(:,100)


DEALLOCATE(A)

END PROGRAM test_matrix_out

它给我以下结果:

   1.000000       2.000000       3.000000       4.000000

   5.000000       6.000000       7.000000       8.000000

  0.0000000E+00   9.000000       10.00000       11.00000

   2.000000       3.000000       4.000000      0.0000000E+00

   6.000000       7.000000       8.000000      0.0000000E+00

  0.0000000E+00  0.0000000E+00  0.0000000E+00  0.0000000E+00

   1.000000       5.000000       9.000000

   2.000000       6.000000       10.00000

 -1.0097448E-28  8.9776148E-39  0.0000000E+00

   4.000000       8.000000       12.00000

  0.0000000E+00  0.0000000E+00  0.0000000E+00

 -3.3631163E-44  1.4293244E-43  0.0000000E+00

为什么会发生?

推荐答案

当您编写A(i,j)时,编译器将计算的地址该内存位置。例如,参见 http://en.wikipedia.org/wiki/Array_data_structure#MultiDimension_arrays。根据语言规则,编译器通常不会确定这是否是合法地址。使用超出维度的索引是非法的。程序员有责任不这样做。 Fortran的优点之一是可以添加针对此错误的运行时检查。传统的传说是运行时下标检查很昂贵,但是当我进行测试时,我经常发现运行时成本可以忽略不计,有时会留在程序的生产版本中。

When you write A(i,j) the compiler calculates the address of of that memory location. See, for example, http://en.wikipedia.org/wiki/Array_data_structure#Multidimensional_arrays. The compiler normally doesn't determine whether this is a legal address according to the rules of the language. Using indices past the dimensions is illegal. It is the responsibility of the programmer not to do this. One of the advantages of Fortran is the ability to add run-time checks for this mistake. The conventional lore is that run-time subscript checking is expensive but I when I have tested I have frequently found the runtime cost to be negligible and sometimes leave it on in production versions of programs.

如果您正在读取内存,则索引错误的可能后果将是获取错误的值,除非内存位置相距太远以致于它不在属于该程序的内存之外,否则将产生错误。如果要写入内存,则将破坏数组其他位置的内存,该内存属于某个其他变量,或者属于程序的内部数据结构。参见缺少重新分配的原因会导致哪些问题?示例问题,其中索引错误导致程序的运行时问题。

If you are reading memory the likely consequence of an index mistake will be to obtain a wrong value, unless the memory location is so far off that it is outside the memory belonging to the program, which will create a fault. If you are writing to memory you will corrupt memory elsewhere in the array, belonging to some other variable, or belonging to an internal data structure of the program. See what kind of problems can lack of deallocation cause? for an example question where an index error caused runtime problems with a program.

这篇关于数组索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆