ifort和超出范围的索引-奇数行为 [英] ifort and out of bound Index - Odd Behaviour
问题描述
最近我恢复了Fortran,也许我缺少了一些东西,但是这种行为看起来很奇怪
Recently I've resumed Fortran and probably there's something I'm missing but this behaviour looks very odd
当我运行以下代码(与ifort编译)时,它只是声明一个数组并设置其元素之一
When I run the following code (compiled with ifort), that just declares an array and sets one of his elements
PROGRAM sol_kernel2
IMPLICIT NONE
INTEGER, PARAMETER :: jpi=5,jpj=5
REAL, DIMENSION(jpi,jpj) :: sshn
PRINT *,jpi,jpj
sshn(10,10) = 150.0
PRINT *,sshn(10,10)
END PROGRAM sol_kernel2
我希望得到一个错误语句,例如SEGMENTATION FAULT,因为我正在尝试使用应该在内存不足的索引来设置sshn变量.我得到的是这样的无错误输出
I expect to get an ERROR Statement, such as SEGMENTATION FAULT, since I'm trying to set the sshn variable using indexes supposed out of memory. I get an error free output like this instead
5 5
150.0000
如果我尝试设置代码
sshn(100,100) = 150.0
我明白了
5 5
0.0000000E+00
无论如何编译器都不会抱怨,但是这次设置为150.0失败.您对此有任何暗示吗?我无法弄清楚自己在做什么错.谢谢
The compiler doesn't complain anyway, but this time 150.0 failed to be set. Have you got any hints about this? I can't figure out myself what I'm doing wrong. Thanks
推荐答案
不需要编译器来诊断您的越界索引.当您执行此类操作时,语言标准未指定会发生什么.也称为未定义的行为.
The compiler is not required to diagnose your out of bound indexing. When you do something like this, what happens is unspecified by the language standard. It is also known as undefined behaviour.
您不能指望发生任何特定的事情.
You can't expect anything specific to happen.
如果您希望诊断出错误,请使用错误检查进行编译:
If you wish your error to be diagnosed, compile with error checking:
ifort -g -traceback -warn -check yourcode.f90
实际发生的事情是您在内存中的某个随机位置写了一些东西.谁知道在更复杂的代码中会发生什么.然后,它可能会在随机情况下崩溃或给出错误的结果.
What actually happened is that you wrote something to some random place in memory. Who knows what would happened in a more complex code. It can then crash at random occasions or give wrong results.
这篇关于ifort和超出范围的索引-奇数行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!