In some places, I saw the use of the OVERLAY keyword in the RPGLE program in data structure sub-fields. For the sub-fields defined using the OVERLAY keyword, there was no definition defined in the definition specs in the RPG program instead the start and end positions were defined on the previous sub-fields. Why do we use this OVERLAY keyword with data structures?
You can also allocate storage of subfields with Keywords
DIM
andOVERLAY
. Here is an example.Explanation of the above source code:
Line 1: Declared an unnamed DS.
Line 2: Declared subfield
word
of length 2o characters.Line 3: Declared another subfield
letter
of length 1 character and usedDIM(20)
keyword to make it an array of 20 elements and also usedOVERLAY
keyword with parameterword
to allocate storage of subfieldword
from start position 1.Line 4: Start of
/Free
block so I can write the code in free-form RPG.Line 5: Evaluate the DS subfield
word
asabcdefghijklmnopqrst
.Line 6:
return
from the program.Line 7: End of /free block using
/end-free
block to specify the end of writing the RPG-free code.Final Outcome:
Since ds subfield
word = ‘abcdefghijklmnopqrst’
therefore, the subsequent subfieldletter
of length 1 and 20 array elements are overlaid on theword
subfield and it would contain:This is the free-form declaration of the above DS subfield that gets allocated using the
DIM
andOVERLAY
keywords.Or
As we all have seen sometimes we use the
OVERLAY
keyword in data structure subfields to overlay the storage of one subfield with that of another subfield.OVERLAY keyword in DS syntax is as follows:
OVERLAY(fieldName:startposition | *NEXT})
Here 2nd parameter
startposition | *NEXT
is optional to use if not specified then it defaults to 1.Start_pos
is specified in terms of bytes, regardless of the subfield type.If specified
OVERLAY(fieldname:startposition)
then that subfield definition overlays thefieldname
parameter storage at the position specified by thestartposition
Optional parameter.If specified
OVERLAY(fieldname:*NEXT)
then it positions the subfield at the next available position within the overlay field.Here is an example of using the
OVERLAY keyword
in data structure subfields.Defining OVERLAY keyword in DS in Fixed form RPG:
Line 1: I declared an unnamed data structure
Line 2: I declared a subfield named FLD1 of length 1o characters.
Line 3: I declared a subfield named FLD2 of length 5 characters, used the OVERLAY keyword with this subfield, and specified FLD1 as the overlaid subfield and start position as 1. Therefore, FLD2 occupies the storage of FLD1 from positions 1 to 5.
Line 4: I declared a subfield named FLD3 of length 5 characters, used the OVERLAY keyword with this subfield, and specified FLD1 as the overlaid subfield and start position as 6. Therefore, FLD3 occupies the storage of FLD1 from positions 6 to 10.
Defining the OVERLAY keyword in DS in free-form RPG:
or
You can define the
OVERLAY
keyword in DS like below:Line 1: Here, I define the data structure named DS1.
Line 2: Declared subfield FLD1 of 5 characters in length.
Line 3: Declared subfield FLD2 of 2 characters and overlaid FLD1 and start position defaults to 1. Therefore, FLD2 occupies positions from 1 to 2 in the FLD1 subfield.
Defining Subfield Overlay Positions with *NEXT:
Line 1: I declared an unnamed data structure.
Line 2: I declared a subfield named FLD1 of length 1o characters.
Line 3: I declared a subfield named FLD2 of length 5 characters used the OVERLAY keyword with this subfield and specified FLD1 as the overlaid subfield and the start position defaults to 1. Therefore, FLD2 occupies the storage of FLD1 from positions 1 to 5.
Line 4: I declared a subfield named FLD3 of length 5 characters and used the OVERLAY keyword with this subfield specified FLD1 as the overlaid subfield and specified *NEXT instead of the start position. Therefore, FLD3 occupies the storage of FLD1 from position 6 to 10 which is available after FLD2 use.
Defining the OVERLAY keyword using *NEXT in DS in free-form RPG:
This can be a good
example
of defining theOVERLAY
keyword in DS.Another example with mixed data type using the
OVERLAY
keyword in DS.You can also look at this example of DS OVERLAY which is complex.
Explanation of the above source code:
Line 1: Declared a DS named
wordDS
.Line 2: Declared subfield named
word
of length 10 characters and usedDIM(5)
keyword to make it an array of 5 elements.Line 3: Declared another subfield named
lastChar
of length 1 character and usedOVERLAY keyword
with a parameterword
to allocate storage of subfield arrayword
from the start position 10 i.e. getting the last character of each array element in this subfieldlastChar
. Please note that thislastChar
subfield is now treated as an array as well since overlaid an array character field.Line 4: Start of
/Free
block so I can write the code in free-form RPG.Line 5-9: Evaluate the DS subfield array
word
elements 1 through 5 with some values.Line 6: return from the program.
Line 7: End of /free block using
/end-free
block to specify the end of writing the RPG-free code.Final Outcome:
Since the DS subfield array
word
containstherefore, the subsequent subfield
lastChar
of length 1 and 5 array elements are overlaid on theword
subfield array from position 1o i.e. the last position of each element of the array and it would contain:This is a unique example of a DS
OVERLAY
.Explanations:
Line 1: Declared an unnamed DS.
Line 2-3: Declared subfield named
months
of length 36 characters and initialized the subfield with starting 3 letters of each month like thisJanFebMarAprMayJunJulAugSepOctNovDec
.Line 4: Declared another subfield named
month
of length 3 characters and usedOVERLAY keyword
with parametermonth
to allocate storage of subfieldmonths
from the start, the position defaults to 1 and also makes it an array of 12 elements using the DIM keyword.Outcome:
Month array each element would contain:
Another good example of DS OVERLAY would be creating a DS on named indicators:
Line 1: Declaring a pointer variable named
Indptr
and initializing it with the address of*IN
, theindicator array
.Line 2: Declaring an unnamed DS which is based on a pointer
Indptr
usingBASED keyword
.Line 3: Declare a subfield named
Indicators
of 99 characters instead of an array.Line 4: Declared indicator subfield named
F03
that overlaysIndicators
subfield storage position 3.Line 5: Declared indicator subfield named
F05
that overlaysIndicators
subfield storage position 5.Line 6: Declared indicator subfield named
F06
that overlaysIndicators
subfield storage position 6.Line 7: Declared indicator subfield named
F09
that overlaysIndicators
subfield storage position 9.Line 8: Declared indicator subfield named
F12
that overlaysIndicators
subfield storage position 12.Line 9: Declared indicator subfield named
sflclear
that overlaysIndicators
subfield storage position 30.Line 10: Declared indicator subfield named
sfldisplay
that overlaysIndicators
subfield storage position 31.