Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

Multi-dimensional Tables: Subscript out of Range

Multi-dimensional Tables: Subscript out of Range

Problem:

When attempting to access a multi-dimensional table outside of its subscript range, a “subscript out of range” error is not always generated at runtime.

Given an array M_TABLE:

01    M_TABLE OCCURS 8.

02   D-TABLE OCCURS 8.

       03   TABLE_DATA   PIC X(5).

And the Procedure Division statement:

MOVE “DATA”   TO TABLE_DATA (x, y).

 

If the values of x and y were 1 and 9 respectively, a ‘subscript out of range’ error is not generated at runtime even though the value in y is greater than the second dimension of the table.

 

The compiler directives that affect subscripting are SSRANGE and BOUND.

http://documentation.microfocus.com/help/topic/com.microfocus.eclipse.infocenter.visualcobol.vs2013/HRCDRHCDIR6Q.html?resultof=%22%73%73%72%61%6e%67%65%22%20

http://documentation.microfocus.com/help/topic/com.microfocus.eclipse.infocenter.enterprisedeveloper.vs2012/HRCDRHCDIR1H.html?resultof=%22%62%6f%75%6e%64%22%20

 

The comments in the BOUND section state:

For multi-dimensional tables, only the composite subscript is checked. If any of the individual subscripts or indices is beyond its limit, but the reference remains within the table, no error is produced.

 

In the example above, as the combined subscript (ie 1,9) is still within the physical bounds of the array, no error is produced at runtime, and the data will still be stored within the tables boundaries. This action means that any data already stored in the table could be overwritten and lost. Unpredictable results may be obtained when this area of the table is accessed further in the program.

Resolution

As the Micro Focus compiler and run time processor can allow an individual subscript of a multi-dimensional table to be outside the range of its own occurrence, it is recommended that checking of subscripts is always coded to ensure this situation does not occur.

For example, in the above table:

 

IF (x < 1 OR > 8)

OR (y < 1 OR > 8)

THEN

               PERFORM SUBSCRIPT_RANGE_ERROR

               GO TO EXIT_PARA

END_IF.

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2016-10-11 15:47
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.