MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Cobol question internal table overflow

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming
View previous topic :: View next topic  
Author Message
vijay
Beginner


Joined: 09 May 2003
Posts: 131
Topics: 64

PostPosted: Wed May 12, 2021 11:49 am    Post subject: Cobol question internal table overflow Reply with quote

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
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12367
Topics: 75
Location: San Jose

PostPosted: Wed May 12, 2021 4:36 pm    Post subject: Reply with quote

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

Code:

CBL SSRANGE


or a run time override as follows

Code:

//STEP0100 EXEC PGM=YOURPGM,PARM='/SSRANGE'

_________________
Kolusu - DFSORT Development Team (IBM)
DFSORT is on the Web at:
www.ibm.com/storage/dfsort

www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Terry_Heinze
Supermod


Joined: 31 May 2004
Posts: 391
Topics: 4
Location: Richfield, MN, USA

PostPosted: Fri May 14, 2021 8:15 am    Post subject: Reply with quote

Vijay,
Neither of your PERFORMs modifies the value of J. That's why it's LOW-VALUES.
_________________
....Terry
Back to top
View user's profile Send private message Send e-mail
misi01
Advanced


Joined: 02 Dec 2002
Posts: 618
Topics: 172
Location: Stockholm, Sweden

PostPosted: Sat May 15, 2021 2:09 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
haatvedt
Beginner


Joined: 14 Nov 2003
Posts: 66
Topics: 0
Location: St Cloud, Minnesota USA

PostPosted: Tue May 25, 2021 10:08 pm    Post subject: Reply with quote

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
View user's profile Send private message
vijay
Beginner


Joined: 09 May 2003
Posts: 131
Topics: 64

PostPosted: Wed May 26, 2021 2:40 pm    Post subject: Reply with quote

Thank you haatvedt. You are right. 01 levels are aligned at double word boundary and giving different results.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group