Highlighted
Absent Member.
Absent Member.
2490 views

[archive] Dynamic change of a variable's length size

[Migrated content. Thread originally posted on 17 September 2003]

The length size of a variable LK-ITEM has to be changed dynamically several times during the execution of a program.
I've tried as below but it did not worked. Some one has already faced similar and solved it?
Thanks in advance.

77 REASON-FOR-EXPORT-ITEM-SIZE pic x(47).
77 CURRENCY-ITEM-SIZE pic x(24).
77 COUNTRY-ITEM-SIZE pic x(40).
. . .
77 TABLE-ITEM-SIZE pic 9(2).
78 ITEM-SIZE value length of TABLE-ITEM-SIZE.
. . .
01 LK-CODES-AREA is external.
02 LK-ITEM pic x(ITEM-SIZE).
. . .
evaluate true
when REASON-FOR-EXPORT-CODES
set TABLE-ITEM-SIZE to size of REASON-FOR-EXPORT-ITEM-SIZE
call "reason-for-export-codes"
end-call
when CURRENCY-CODES
set TABLE-ITEM-SIZE to size of CURRENCY-ITEM-SIZE
call "currency-codes"
end-call
when COUNTRY-CODES
set TABLE-ITEM-SIZE to size of COUNTRY-ITEM-SIZE
call "country-codes"
end-call
. . .
0 Likes
6 Replies
Highlighted
Absent Member.
Absent Member.

RE: [archive] Dynamic change of a variable's length size

Hm...
I don't believe you can do what you try to accomplish. But it would be better if you explained the context.
I assume these calls you are doing are to C functions? May I ask what these do, and if you have developed them internally, why would you use external variables instead of passing parameters, also wondering why the need for variable sizing, why not just use "one size fits all", these days memory are thrown at your feets for nothing anyway.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Dynamic change of a variable's length size

Of course a task can be accomplished in a variety of different ways.
But consider the following scenario.
There are about 30 old COBOL, existing programs, all using same external variable's names ITEM-CODE and ITEM-DESCRIPTION but in each program they are different size as each of them is working with different table of code/description.
Assuming that those 30 old programs has NOT (or can not) to be changed. A new COBOL program has to call all of them, one by one in the procedure division, facing 2 facts (problems):
- due of external clause ITEM-CODE/DESCRIPTION have to appear only once in the working-storage?
- executing COBOL calls in procedure division should be done with ITEM-CODE/DESCRIPTION size appropriate to calling program, meaning different size for each call.

There would be no problem if those 30 programs CAN BE changed, right? The easiest way would be to make them callable just by using linkage section and in the new program which has to call them use 30 appropriate ITEM-CODE/DESCRIPTION pears, each different in name and size.

However,thanks for interesting deeper into this.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Dynamic change of a variable's length size

The only solution I can think of for this, is to use dynamic memory.
E.g. Assuming your application is the one where the real allocation is taking place, so that the EXTERNAL clause appears in the other applications, I would make the definition in your app to be a PIC X whatever, then, upon the call to each subsequent program, I would dynamic memory for each call and set the address of the external item to this allocation, like:

77 MY-EXTERNAL PIC X.
77 DYNAMIC-PTR USAGE POINTER.
77 MY-REAL-1 PIC X(200).
77 MY-REAL-2 PIC X(300).
* and so on...

PROCEDURE DIV...

CALL "M$ALLOC" USING 200 DYNAMIC-PTR.
SET MY-EXTERNAL TO DYNAMIC-PTR.
CALL "external application to do whatever"
CALL "C$MEMCPY" USING BY REFERENCE MY-REAL-1
BY-VALUE DYNAMIC-PTR 200.
CALL "M$FREE" USING DYNAMIC-PTR.
*Repeat for next program...
CALL "M$ALLOC" USING 300 DYNAMIC-PTR.
SET MY-EXTERNAL TO DYNAMIC-PTR.
CALL "external application to do whatever"
CALL "C$MEMCPY" USING BY REFERENCE MY-REAL-2
BY-VALUE DYNAMIC-PTR 300.
CALL "M$FREE" USING DYNAMIC-PTR.
* And so on...

Hope this helps.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Dynamic change of a variable's length size

Question:
How come you don't set your external data area to the largest possible size?

Merlin
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Dynamic change of a variable's length size

As I said ...

"...There are about 30 old COBOL, existing programs, all using same external variable's names ITEM-CODE and ITEM-DESCRIPTION but in each program they are different size as each of them is working with different table of code/description.
Assuming that those 30 old programs has NOT (or can not) to be changed..."

... so the programs have to be used as they are, no chance of changing them.

Thanks and best regards Merlin
Larry
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Dynamic change of a variable's length size

Is there a set number of sizes you are working with? Or could it be any size. A simple solution would be:

01 My-Table pic x(n) is external.
03 My-Table-Size-02 redefines My-Table.
05 My-Table-02-Row occurs n/2 times
03 My-Table-Size-05 redefines My-Table.
05 Mt-Table-05-Row occurs n/5 times.

make sure you

initilize My-Table

when you switch table sizes.

The problem is you need to have to declare all your various table sizes at compile time. In your case it should work though.

Merlin
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.