View previous topic :: View next topic |
Author |
Message |
vijay Beginner
Joined: 09 May 2003 Posts: 131 Topics: 64
|
Posted: Wed May 12, 2021 11:49 am Post subject: Cobol question internal table overflow |
|
|
Hello all,
I have a question with below code. Why is the output different for both code fragments? I expected value of 'ZZ' in field 'J' in both cases.
Code: | WORKING-STORAGE SECTION.
01 I PIC 9(02) VALUE ZEROS.
01 W-TBL.
05 FILLER OCCURS 04 TIMES.
10 VAL PIC X(02).
01 J PIC 9(02).
01 K PIC 9(02).
PROCEDURE DIVISION.
PERFORM VARYING I FROM 1 BY 1
UNTIL I > 05
MOVE 'ZZ' TO VAL(I)
DISPLAY I ' : ' VAL(I) ' : ' J
END-PERFORM.
DISPLAY 'J: ' J
|
J: ZZ
Code: | WORKING-STORAGE SECTION.
01 I PIC 9(02) VALUE ZEROS.
01 W-TBL.
05 FILLER OCCURS 10 TIMES.
10 VAL PIC X(02).
01 J PIC 9(02).
01 K PIC 9(02).
PROCEDURE DIVISION.
PERFORM VARYING I FROM 1 BY 1
UNTIL I > 11
MOVE 'ZZ' TO VAL(I)
DISPLAY I ' : ' VAL(I) ' : ' J
END-PERFORM.
DISPLAY 'J: ' J
|
J: LOW-VALUES
Thanks,
Vijay |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
kolusu Site Admin
![Site Admin Site Admin](images/ranks/adminpic.jpg)
Joined: 26 Nov 2002 Posts: 12369 Topics: 75 Location: San Jose
|
Posted: Wed May 12, 2021 4:36 pm Post subject: |
|
|
vijay,
In both cases you are writing beyond the occurs value. Depending on how the storage is obtained by the compiler you are seeing different results.
Run the program with SSRANGE which does a validation of the subscripts and abends right away after you passed the limit.
Add this 1 line before the program
or a run time override as follows
Code: |
//STEP0100 EXEC PGM=YOURPGM,PARM='/SSRANGE'
|
_________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
Terry_Heinze Supermod
Joined: 31 May 2004 Posts: 391 Topics: 4 Location: Richfield, MN, USA
|
Posted: Fri May 14, 2021 8:15 am Post subject: |
|
|
Vijay,
Neither of your PERFORMs modifies the value of J. That's why it's LOW-VALUES. _________________ ....Terry |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
misi01 Advanced
Joined: 02 Dec 2002 Posts: 624 Topics: 175 Location: Stockholm, Sweden
|
Posted: Sat May 15, 2021 2:09 am Post subject: |
|
|
As a matter of "good" programming practices, I always try and avoid hard-coding the number of loops. Consider the following:-
Code: |
01 table1.
02 table1-rows occurs 10.
03 table1-details pic XX.
01 table2.
02 table2-rows occurs 10.
03 table2-details pic XX.
|
Now consider that the program needs to change the number of rows in table1 to 15.
Oh well, all I have to is change all 10's to 15's. Ooops, just ruined the program logic.
Now consider the following
Code: |
Compute nr-table1-rows = length of table1
/ length of table1-rows(1)
Compute nr-table2-rows = length of table2
/ length of table2-rows(1)
|
Now if you use nr-tablen-rows for your perform loops, all you have to do is change the occurs in table1 and you're done. _________________ Michael |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
haatvedt Beginner
Joined: 14 Nov 2003 Posts: 66 Topics: 0 Location: St Cloud, Minnesota USA
|
Posted: Tue May 25, 2021 10:08 pm Post subject: |
|
|
I believe that Enterprise COBOL would align "01" levels at a 16 byte boundary.
Check the address of W-TBL and J to see if that is the case. If so that explains why J was not overlaid by your code.
Change your perform loop to use UNTIL I > 10 and see what you get. _________________ Chuck Haatvedt
email --> clastnameatcharterdotnet
(replace lastname, at, dot with appropriate
characters) |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
vijay Beginner
Joined: 09 May 2003 Posts: 131 Topics: 64
|
Posted: Wed May 26, 2021 2:40 pm Post subject: |
|
|
Thank you haatvedt. You are right. 01 levels are aligned at double word boundary and giving different results. |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
|
|