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
如果我尝试在代码中设置
If I try to set in the code
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屋!