Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

arrays - ifort and out of bound Index - Odd Behaviour

Recently I've resumed Fortran and probably there's something I'm missing but this behaviour looks very odd

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

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

I get this

           5           5
0.0000000E+00

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...