http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54788



--- Comment #3 from Tobias Burnus <burnus at gcc dot gnu.org> 2012-10-03 
09:14:26 UTC ---

(In reply to comment #0)

>   allocate(vec(2))

>   allocate(vec(0)%arr(4,4))



I assume you have a C background. In Fortran, by default the lower array bound

is one. Thus, with "allocate(vec(2))" you allocated an array of size 2, which

can be accessed via vec(1) and vec(2). Thus, you access invalid memory for

"vec(0)".



However, you can also allocate via

   allocate(vec(0:2))

then you have a size-3 array, where vec(0), vec(1) and vec(2) are valid.

(you could also have negative bounds, e.g. "allocate(vec(-2:0))".)





The other point is, as Janus already mentioned: Fortran only has pointers to

arrays but not an array of pointers. Thus "vec(1)" and "vec(2)" point to the

same array, one to the first element and one to the second element.



In terms of the standard,

  allocate(vec(2))

allocates an anonymous target (a rank-1, size-2 array) and then pointer assigns

that target to the (scalar) pointer "vec". Thus "vec" is a pointer to a size-2

array with the lower bound "1" and upper bound "2".





(In reply to comment #2)

> Thus, if you want to do bounds remapping, you have to specify a range, e.g.

>   vec(1:1) => vec(0:0)



Remark to my example: While it is syntactically correct, accessing "vec(0:0)"

isn't (out of bounds). It were okay for "allocate(vec(0:2))" - but still, it

wouldn't do what you expect:

"vec(1)" would then point to the first element of the array, "vec(2)" would be

in principle the second element (and in practice accessible), but but one may

not use it: In "vec(1:1) =>" one has explicitly told the compiler that one only

wants to point to a size-1 array.

Reply via email to