Skip to main content

linkage section memory freeing

  • January 26, 2011
  • 3 replies
  • 0 views

[Migrated content. Thread originally posted on 24 January 2011]

Hi all. During the processing of a called program, i perform this instruction:
set address of x to null
where x is a linkage variable declared by the using verb and passed by value:
procedure division using x
My question is, does the memory of x is correctly released when i use the cancel verb from the calling program, even if i set its address to null?

3 replies

[Migrated content. Thread originally posted on 24 January 2011]

Hi all. During the processing of a called program, i perform this instruction:
set address of x to null
where x is a linkage variable declared by the using verb and passed by value:
procedure division using x
My question is, does the memory of x is correctly released when i use the cancel verb from the calling program, even if i set its address to null?

An item passed by linkage is passed by reference and managed by the calling program. E.g. if you have declared x in program A, program A calling program B passing x, the instance of x is in program A. What program B sees, is the address of the item, but it does not manage its memory. When you receive item x in program B, it is actually a pointer, not the data item.

So, when you do an assignment of x to another address in program B, you are changing the address, not the original item. This change is local to program B and will not be seen outside of program B.

Thus, when program B is cancelled, so is all its local resources, but not what item x originally pointed to, which was passed with the CALL. This is still in program A. When you return from program B to program A, x will be the same as before the CALL.

example Mother.cbl:

       IDENTIFICATION               DIVISION.
       PROGRAM-ID.                  MOTHER.

       WORKING-STORAGE SECTION.
       77 MotherItem PIC X(10) VALUE "Mother".

       PROCEDURE DIVISION.
       MAIN-LOGIC.
           CALL    "Child"          USING
                   MotherItem
      *When we get to the STOP RUN, MotherItem still is "Mother", thus a CANCEL "Child" have no effect on MotherItem.
           STOP    RUN
           .


Example Child.cbl:

       IDENTIFICATION               DIVISION.
       PROGRAM-ID.                  CHILD.

       LINKAGE SECTION.
       01 PassedItem                PIC X(10).

       PROCEDURE DIVISION USING PassedItem.
       MAIN-LOGIC.

           Set Address of PassedItem to NULL. | This has only local effect to program Child.
           GOBACK
           .


CANCEL "Child", will release any resources in Child. Including whatever PassedItem has been reassigned to in program Child, but it has no effect on MotherItem, as we are modifying the pointer not the actual data.

Gisle

[Migrated content. Thread originally posted on 24 January 2011]

Hi all. During the processing of a called program, i perform this instruction:
set address of x to null
where x is a linkage variable declared by the using verb and passed by value:
procedure division using x
My question is, does the memory of x is correctly released when i use the cancel verb from the calling program, even if i set its address to null?

Thank you for the answer, but as specified the question was about the case of passing x by content (actually i wrongly wrote BY VALUE, but i meant BY CONTENT, sorry). Does your answer applies also to this case?

from the acu doc:

The BY CONTENT phrase is similar to BY REFERENCE, except that the address of a copy of the data item is sent. This has the effect that any changes made to the parameter in the called program are not seen by the caller. Starting with Version 2.0, literals passed as parameters are automatically passed BY CONTENT


Thank you.

[Migrated content. Thread originally posted on 24 January 2011]

Hi all. During the processing of a called program, i perform this instruction:
set address of x to null
where x is a linkage variable declared by the using verb and passed by value:
procedure division using x
My question is, does the memory of x is correctly released when i use the cancel verb from the calling program, even if i set its address to null?

Yes, BY CONTENT have the same effect. Although, as the documentation states, this is a copy of the original element. Practically making the original item read/only. I mean, you can write to the item passed BY CONTENT, but this writing is never reflected in the original item.

The temporary buffer for passing BY CONTENT is bound to the CALL and released when the CALL return, independent of CANCEL.

I'm sorry I missed that you had written BY VALUE, but that would certainly not affect the original item.