Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29282 Discussions

Allocating an array to a negative size does not trigger an error!?

OP1
New Contributor III
1,397 Views
The following command ALLOCATE(MY_ARRAY(-10),STAT=ERROR) will not return an error (ERROR=0 and MY_ARRAY is allocated to size 0).
This is bit bizarre!? Why was the ALLOCATE commandimplemented in such a way? It *would* seem more logical to trigger an error for this type of scenario.

Just out of curiosity...

Olivier
0 Kudos
3 Replies
Lorri_M_Intel
Employee
1,397 Views
Quoting - opmkl
The following command ALLOCATE(MY_ARRAY(-10),STAT=ERROR) will not return an error (ERROR=0 and MY_ARRAY is allocated to size 0).
This is bit bizarre!? Why was the ALLOCATE commandimplemented in such a way? It *would* seem more logical to trigger an error for this type of scenario.

Just out of curiosity...

Olivier

Bizarre? Maybe ...

However, this behavior is exactly specified in the standard.

This is from Section 6.3.1 ALLOCATE statement. I bolded the section of the paragraph that applies to your example.

When an ALLOCATE statement is executed for an array, the values of the lower bound and upper bound expressions determine the bounds of the array. Subsequent redefinition or undefinition of any entities in the bound expressions do not affect the array bounds. If the lower bound is omitted, the default value is 1. If the upper bound is less than the lower bound, the extent in that dimension is zero and the array has zero size.

I hope this helps --

- Lorri

0 Kudos
OP1
New Contributor III
1,397 Views
Thanks Lorri for pointing out at this section of the standard. I just had to track down a bug in my code because of this. The variable that specifies the size of my allocatable array had become negative prior to the allocation itself- and I didn't think Fortran would not be able to catch this. So now I just need to add one more test for my allocations.

Thanks!

Olivier

0 Kudos
Peter
Beginner
1,397 Views
Quoting - opmkl
Thanks Lorri for pointing out at this section of the standard. I just had to track down a bug in my code because of this. The variable that specifies the size of my allocatable array had become negative prior to the allocation itself- and I didn't think Fortran would not be able to catch this. So now I just need to add one more test for my allocations.

Thanks!

Olivier


Oliver,

I am wondering if you have Check array and string bounds on. (Project>YourProject properties>Fortran>Runtime>Check Array and string bounds).

If not, turn it on (Yes(/check:bounds)) and see what it does.
0 Kudos
Reply