Hi,
I am trying to allocate only the needed amount of memory when working with static tables in cobol. The table(which is part of a data-area) is then used as a parameter in json generate statement.
Suppose there are two programs. Program TestA and TestB. TestA calls TestB which is the one responsible to allocate n amount of memory depending on the size of the table. This means that even though the table has a boundary of 50000 items I want that the amount of byte allocated to the data-area should be less than that(depending on the amount of items populated which is known only in runtime).
Below is the an example:
program-id. TestA as "TestA" environment division. configuration section. data division. working-storage section. 01 foo-pointer. 04 tmp-ptr pointer. 1 tbl-size pic 9(8) value 0. 1 outputarea pic x(53400356) value space. linkage section. 1 fooArea. 2 fooResponse. 3 fooRespOkPayload. 4 fooRespOkCode pic x(10) value space. 4 fooRespOkMessage pic x(100) value space. 4 fooRespObject. 5 fooRespObjectName pic x(50). 5 fooRespList occurs 50000 depending on tbl-size. 6 fooRespObjectOwnership pic x(40) value space. 3 fooRespFailPayload. 4 fooRespFailCode pic 9(6) value 0. 4 fooRespFailMessage pic x(100) value space. $set sourceformat "fixed" procedure division. call "TestB" using fooArea foo-pointer tbl-size set address of fooArea to tmp-ptr if tmp-ptr = null display "Null" stop run end-if json generate outputarea from fooArea on exception display "Error generating json using dynamic allocation" end-json display outputarea free tmp-ptr cancel "TestB" goback end program TestA.
copy "cblproto". program-id. TestB as "TestB". environment division. configuration section. data division. working-storage section. 1 x pic 9(8). 1 totalLengthOfFoo pic 9(15) value 0. 1 lgnthOfSingleItem pic 9(15) value 0. 1 maxOccrTbl pic 9(8) value 0. 1 sizeOfOnlyTbl pic 9(8) value 0. 1 sizeOfSingleElem pic 9(8) value 0. 1 byteToAllocate pic 9(15) value 0. 1 remainderSizeOfInput pic 9(8) value 0. 1 realSizeOfSingleElem pic 9(8) value 0. linkage section. 1 fooArea. 2 fooResponse. 3 fooRespOkPayload. 4 fooRespOkCode pic x(10) value space. 4 fooRespOkMessage pic x(100) value space. 4 fooRespObject. 5 fooRespObjectName pic x(50). 5 fooRespList occurs 50000 depending on tbl-size. 6 fooRespObjectOwnership pic x(40) value space. 3 fooRespFailPayload. 4 fooRespFailCode pic 9(6) value 0. 4 fooRespFailMessage pic x(100) value space. 01 foo-pointer. 04 tmp-ptr pointer. 1 tbl-size pic 9(8) value 0. procedure division using fooArea foo-pointer tbl-size. display "TestB" move 50000 to x tbl-size move length of fooArea to totalLengthOfFoo move length of fooRespList to lgnthOfSingleItem*> 40 display length of fooRespObject display length of fooRespObjectName compute maxOccrTbl = (length of fooRespObject - function length(fooRespObjectName)) / lgnthOfSingleItem compute sizeOfSingleElem = totalLengthOfFoo / maxOccrTbl compute sizeOfOnlyTbl = lgnthOfSingleItem * maxOccrTbl compute remainderSizeOfInput = totalLengthOfFoo - sizeOfOnlyTbl move 2 to x compute byteToAllocate = (sizeOfSingleElem * x + remainderSizeOfInput) allocate byteToAllocate characters returning tmp-ptr if tmp-ptr = null display "Could not allocate mem" end-if set address of fooArea to tmp-ptr initialize fooArea(1:byteToAllocate) move "Workshop" to fooRespObjectName move "Test1" to fooRespObjectOwnership(1) move "Test2" to fooRespObjectOwnership(2) move x to tbl-size goback end program TestB.
The result of the json generate can be seen below
I do believe that not enough data is allocated because when I try to assign a value to fooRespFailMessage after setting address of fooArea to tmp-ptr I get the 114 error code(Attempt to access item beyond bounds of memory).
Any help is appreciated.