While the following code type b integer, allocatable :: i(:) end type b type(b), allocatable :: a(:) !allocate(a(10)) print *, allocated(a)!, allocated(a(1)%i) !deallocate(a) end seems to be a valid f2003 code and gives 'F' at run time, replacing print *, allocated(a)!, allocated(a(1)%i) with print *, allocated(a), allocated(a(1)%i) gives a "Segmentation fault" at run time (it gives 'T F' if the allocate/deallocate lines are uncommented). When asked on IRC about this kind of inquiry of a component of an unallocated derived type, Tobias Burnus answered immediately that such a use is invalid, pointing to 2.4.9 Allocatable variables ... 2 An unallocated allocatable variable shall not be referenced or defined. ... Then he made the comment that (obviously) this restriction does not apply for 13.7.11 ALLOCATED (ARRAY) or ALLOCATED (SCALAR) 1 Description. Query allocation status. 2 Class. Inquiry function. 3 Arguments. ARRAY shall be an allocatable array. SCALAR shall be an allocatable scalar. 4 Result Characteristics. Default logical scalar. 5 Result Value. The result has the value true if the argument (ARRAY or SCALAR) is allocated and has the value false if the argument is unallocated. but that 'a(1)%i' is not an array but an invalid expression per 2.4.9 that cannot be a valid argument of allocated. Note that my original problem came from scalar allocatable and that the above was less obvious for 'a%i'. It would be nice to have a run time check for such invalid use of unallocated allocatable variables (such as -fcheck=use_unalloc).
Forwarded from http://gcc.gnu.org/ml/gcc-bugs/2010-10/msg02167.html > > It would be nice to have a run time check for such invalid use of > > unallocated allocatable variables (such as -fcheck=use_unalloc). > If you use an unallocated variable you get a segmentation fault. > Isn't this a sufficient runtime check ? Well! You can say the same thing for -fcheck=bounds if writing outside array bounds gives you a segmentation fault! Nevertheless nobody will argue that this check is useless: it can save you hours of debugging to locate the line(s) in which you do it. Segmentation faults tell you that there is something wrong, but not why and/or where (not counting gfortran bugs). It took me some time to understand it for a less than 20 line code in which I had the bad idea of using a copy and paste from a valid line to an invalid one (this can happen to anybody with kloc!-)
(In reply to comment #1) > Well! You can say the same thing for -fcheck=bounds if writing outside array > bounds gives you a segmentation fault! Nevertheless nobody will argue that this > check is useless: it can save you hours of debugging to locate the line(s) in > which you do it. Well, my point was that an out of bound access doesn't necessarily faults whereas a NULL pointer access always does. Anyway, it can't do any harm. Confirmed.
Another variant of pr20520. *** This bug has been marked as a duplicate of bug 20520 ***