Compatibility of array indexing for gcc 3.4.6 and gcc 4.4.7?
Steve Kargl
sgk@troutmask.apl.washington.edu
Fri Nov 1 14:59:00 GMT 2013
On Fri, Nov 01, 2013 at 03:07:08PM +0100, Stefan Schmitt wrote:
>
> I think I found the reason why the code does not crash on gcc 3.4.6.
>
> The index I was using, index=-2147483647, corresponds to 0x80000001 in
> hex. When used on a 32-bit system to address a machine word, the upper
> two bits are lost. So the example code is equivalent to index=1 on a
> 32-bit machine, and the first word of the array is returned.
>
> Maybe you are also interested to see where this particular index came
> from. The original code fragment looks approximately like this:
> r=sqrt(x**2+y**2)
> index=int(r)+1
> ...and this was called with x=y=+infinity. Converting +infinity to int
> gives 0x80000000 and then +1 is added ...
>
> Still I have the question whether there is a compiler flag to ensure
> compatibility with 32 bit addressing.
>
AFAIK, no.
Does your question actually apply to what you describe? If you have
a 32-bit entity and you lose the upper 2 bit, wouldn't that be 30-bit
addressing?
If x or y can be arbitrarily large, it would probably be prudent
to fix the code. The standard forbids calling an intrinsic
function with an argument that would cause the result to be
outside the representable range of the return type for the function.
You need something like
if (r > huge(index) - 1)
index = 1
else
index = int(r) - 1
--
Steve
More information about the Fortran
mailing list