Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Arrays in COBOL

Status
Not open for further replies.

ashykhanna

Programmer
May 27, 2002
14
IN
Hi,

Can anyone please help me with a piece of code on the declaration and usage of dynamic array in cobol.

For instance,
i have a esds vsam file of 100 records usually fed in as input, i use a internal table for storing the values from each record in an array (occurs 100 times). wat if the file has now 200 records, how can i increase the array size at run-time.

Thanks,
Ashok
 
ashykhanna

If by an array you just mean a 01 (or other) level in working storage with an OCCURS clause, then I don't think you can change it's size during runtime. Cobol is not very good a dynamically allocating memory (that I know of).

The other thing you could do as allocate the memory yourself (we do this in AcuCobol, so I'm assuming you could do something similar on the mainframe) and create yourself a linked list or collection. There must be some Cobol function you can use on your platform to dynamically allocate and deallocate memory ...

This would be quite a bit of work, though, so I don't know if it's worth it to you. Managing memory can be a tricky thing to do sometimes.

.DaviD.
 
I don't think you can increase the array size at runtime. However, you can make OS calls to accomplish this.

__________________________________________
Try forum1391 for lively discussions
 
From the reference to "ESDS" - I assume that you are talking about an IBM mainframe environment. If so, I would allocate the table in Linkage Section (not Working-Storage) and use the LE CALLABLE service:

CEEGTST

See:

to allocate the storage.

You would define the Linkage Section item with an ODO(Occurs Depending On) phrase.

This would work BEST if you have a way to determine (at run-time but before doing the allocation) how many entries you will have. You *could* do this "dynamitically* as you read each input record, but that wouldn't be very efficient/easy to do.

Bill Klein
 
Wait. What is the purpose of the values in this ESDS file? I would seriously look at that and consider other options here - personally I smell a bad design, but I'm not too sure on what I see here of that.

Is there another way to tackle this problem? I wouldn't be surprised though if this is just someone modifying a bad design that's been carried over the years.
 
Hi,
The program runs fine for months, but one fine day it gets more than 100 records (approx) and hence abends. I do agree that this problem had to taken into consideration during design itself, but now client does not want this file design to be altered as its been coming from some other system and many other programs use this file.

WMK,
Thanks for the tip. I shall try it out keep you updated.

Regs,
Ashok
 
You said:
"I do agree that this problem had to taken into consideration during design itself, but now client does not want this file design to be altered as its been coming from some other system and many other programs use this file."

Irregardless, what is the purpose of these values in the program? I refer to the design of this program not the layout of the file - I can't think of any reasons in 99.9999999% of all the cases I've ever seen to preload a file into an array like you are doing.
 
>i have a esds vsam file of 100 records usually fed in as input, i use a internal table for storing the values from each record in an array (occurs 100 times). wat if the file has now 200 records, how can i increase the array size at run-time.

The COBOL solution is to define the table as OCCURS 1 TO Max-Size DEPENDING ON Subscript.

You must specify Max-Size as a number reasonably higher than the largest size anticipated. When adding records to the table, first ADD 1 TO Subscript, then add an entry into the table.

__________________________________________
Try forum1391 for lively discussions
 
I must reluctantly agree with Dimadja (no pun intended; I just hate it that Cobol can't be more dynamic than this).

I think the original question was about a finding a completely dynamic way of doing this, so that you never need to worry about a top limit. I don't see how that can be done, though, so I think your solution is probably the one involving the DEPENDING ON.

.DaviD.
 
Just make the storage allocation larger for the table (COBOL FOR ARRAY). A lot depends on how large your array is for one table entry. In standard COBOL there are storage limits for a table/array. You could actually read the file every time dynamically, but that may be too time-consuming.

Instead of having an Abend you need to add programming to test for the table size so the program and just give you an error warning if the table size is too large. It is better if the programmer catches the errors than having an abend which creates a dump.

If you do not like my post feel free to point out your opinion or my errors.
 
Another way is to play with a disk based array like a RELATIVE file created from the input file.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top