DRELEASE Examples


Example 1:

 

pDimA

DIM

^

.

 

 

  

DMAKE

pDimA,1000000

  

.

 

  

.program executes

  

.

 

  

DRELEASE

pDimA ;DIM variable buffer is freed

  

  

         ;pDimA pointer is cleared

  

STOP

 

 

This example shows the simple creation and destruction of a large DIM variable.

Example 2:

 

pDimA

DIM

^

pDimB

DIM

^

.

 

 

  

DMAKE

pDimA,1000000

  

MOVEPTR

pDimA,pDimB ;Both point to DIM variable

  

.

 

  

.program executes

  

.

 

  

DRELEASE

pDimB ;DIM variable buffer is not freed

  

  

         ;Pointer pDimB is cleared

  

  

         ;Pointer pDimA is not cleared

  

STOP

 

 

In this example, the large variable created is associated with a second pointer variable. Note that when the second pointer is released, the original pointer is still valid and the referenced memory is unchanged. Only the second pointer is destroyed.

Example 3:

 

pDimA

DIM

^

dVar

INIT

"Testing"

.

 

 

  

DMAKE

pDimA,1000000

  

CALL

xFunc using pDimA

  

.

 

  

.program executes

  

.

 

  

DRELEASE

pDimA ;DIM variable buffer is not freed

  

  

         ;Pointer pDimA is cleared

  

  

         ;Pointer pDimC still points to

  

  

         ; DMAKE buffer allocation

  

.

 

  

.program continues

  

.

 

  

CALL

xFunc using dVar

  

.

 

  

. After the last xFunc operation, the DIM variable

  

. for the DMAKE operation is freed because the

  

. pDimC pointer was changed to point to dVar and

  

. no other pointers pointed to the DMAKE buffer.

  

.

 

  

STOP

 

.

 

 

pDimC

DIM

^

.

 

 

xFunc

LROUTINE

pDimC

  

.

 

  

.pDimC points to input DIM variable

  

.

 

  

RETURN

 

 

This example show a more complicated scenario. A large variable is created and the pointer (pDimA) associated with it is passed to a subroutine. The process of passing the parameter creates a new pointer (pDimC) and associates it with the large variable. Upon returning from the routine, the program releases the first pointer (pDimA). The memory occupied by the large variable is not release since the pointer defined in the subroutine (pDimC) still references it. The second call to the subroutine passes in new value for pDimC. This implicity destroys the pointer which in turn releases the original memory before it is assigned to the new variable.

 



PL/B Language Reference DRAGITEM Example EDIT Examples