Simple problem on array indices

Adam Hirst adam@aphirst.karoo.co.uk
Thu Jan 9 12:38:00 GMT 2014


On 09/01/14 12:14, Hom Nath Gharti wrote:
>    integer :: inds(4),icount(3)
>
>    inds = (/ 1, 2, 2, 3 /)
>    icount = 0
>
>    icount(inds) = icount(inds) + 1

I'm no expert, but this sounds it might be a quirk of how the compiler 
generates the loop to add 1 to the entries of icount; perhaps whether or 
not it generates a temporary, or whether it assigns the results of each 
individual +1 to icounts *before* evaluating another sum.

`icount(inds)` is the same as
icount( [1, 2, 2, 3] ) = [ 0, 0, 0, 0 ]

But because of that 2 appearing twice, it's not clear whether you're
1) adding 1 to both instances of icount(2), and since they're both 0, 
both instances icount(2) (in fact, the same actual value) should be set to 1
or
2) adding 1 to the first instance of icount(2), making it 1, and then 
adding 1 to the second instance of icount(2), making it 2

I'd be surprised if weren't rules in the standard about whether 
array-valued indices are allowed to alias, because if they are it just 
sounds like it's allowing all kinds of ambiguous problems like this.

I look forward to what other people in the list have to say, this is 
interesting.

~ Adam



More information about the Fortran mailing list