I have written this RPGLE code having a DS defined with overlay keyword
and some of its subfields are varchar fields
.
The source code is as follows:
D ds1 DS
D fld1 10A varying //4+1+4 = 9
D fld2 4A varying overlay(fld1)
D fld3 1a overlay(fld1:*next)
D fld4 4a varying overlay(fld1:*next)
/free
fld1 = 'abcdefghij';
return;
/end-free
Line 1: Declare a DS named DS1
.
Line 2: Declare subfield named fld1
of 10 varchar.
Line 3: Declare subfield named fld2
of 4 varchar and overlays fld1
storage area.
Line 4: Declare subfield named fld3
of 1 character and overlays *next
available storage area in fld1
.
Line 5: Declare subfield named fld4
of 4 varchar and overlays *next
available position of fld1
.
Line 6: Start of /free
block to write rpg-free code after this line.
Line 7: Evaluate fld1 = ‘abcdefghij’;
Line 8: Return from program.
Line 9: End of the free block by specifying /end-free
block.
DS has 2 varchar fields and the declared size is 10, I get the following error during compile.
*RNF7303 30 1 Subfield defined with keyword OVERLAY is too big;
specification ignored.
then I realized that varchar had a variable part of 2 bytes which stores varchar variable actual length. So, I have 2 Varchar fields
overlaid and each requires 2 bytes. So the total length would be = 4+2+1+4+2 = 13 bytes i.e. varchar(11) instead of varchar(10). Since varchar(11) means (11+2 = 13).
Therefore, I declared the subfield fld1
as 11 varchars, and the program now compiles successfully
.
D fld1 11A varying
Below is the output that I received:
EVAL ds1
FLD1 OF DS1 = 'abcdefghij '
FLD2 OF DS1 = 'abcd'
FLD3 OF DS1 = 'e'
FLD4 OF DS1 = 'hij '
My expected output was:
EVAL ds1
FLD1 OF DS1 = 'abcdefghij '
FLD2 OF DS1 = 'abcd'
FLD3 OF DS1 = 'e'
FLD4 OF DS1 = 'fghi'
In the subfield FLD4
of DS1
, it’s picking the wrong value and not handling it correctly. Please suggest if we can use VARCHAR
subfields with DS OVERLAY
.
We should avoid
overlay varchar subfields
over othervarchar subfields
.