Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29235 Discussions

array bounds mismatch slips thru IVF?

forall
Beginner
788 Views
I was checking my code using IVF vs CVF and came across the following issue.

allocate(tsA%name(nX)); tsA%name=xName0

I hit this statement where nX=2 and xName0 is an array with a single element (ie, previously allocated with xName0(1)). Note that 'name' is a pointer whereas 'xName0' is allocatable (both characters).

The size mismatch is essentially a bug on my part, but I was curious to see that under IVF this simply results in tsA%name(1)=xName0(1) and tsA%name(2) is left unaffected. I checked and in IVF I do have the "Yes (/check:bounds)" option.

Under CVF this causes an access-violation, which is how I found the problem.

Which behaviour is correct/standard? I expected (and wanted) this to be an error, but Fortran-2003 allows some "behind-your-back" array resizing so I am no longer sure ..

Is there some way of making IVF give an error in this scenario?

thanks in advance, d


0 Kudos
3 Replies
Steven_L_Intel1
Employee
788 Views
In IVF, if you add /assume:realloc_lhs, then tsa%name will get automatically reallocated to the shape of xName0. Without that, as with CVF (which doesn't have the option), the results when the shapes don't match are unpredictable.

The mismatch is not an error in Fortran 2003, since tsa%name is an allocatable array. Otherwise, what you want is "shape checking", which neither compiler does. Array bounds checking is not involved here as there are no subscripts.
0 Kudos
forall
Beginner
788 Views
In IVF, if you add /assume:realloc_lhs, then tsa%name will get automatically reallocated to the shape of xName0. Without that, as with CVF (which doesn't have the option), the results when the shapes don't match are unpredictable.

The mismatch is not an error in Fortran 2003, since tsa%name is an allocatable array. Otherwise, what you want is "shape checking", which neither compiler does. Array bounds checking is not involved here as there are no subscripts.

I am curious why shape checking is not done by either compiler. Does it not just require precomputing the implied bounds (which is not hard?) and then just doing the usual bounds check?

I suspect doing the shape check would unravel quite a few hidden bugs.. It would also reduce the potential for "unpredictable" behaviour, which somehow is worse than an error from the point of view of a programmer.

I can understand I can do the check myself in the code but that would become cumbersome to do manually everytime, especially if I wanted to disable the check for Release.
0 Kudos
Steven_L_Intel1
Employee
788 Views
Technically, it's straightforward, but it is a significant amount of added code and other items have been a higher priority. I'll add you to the list of those who have asked for it. It's something I'd like to see as well.
0 Kudos
Reply