IBM AS/400 Frozen Dessert Maker User Manual


 
Managing Dynamically-Allocated Storage
Note: Although the ALLOC operation code works only with the default heap, the
REALLOC and DEALLOC operation codes work with both the default heap
and user-created heaps.
Figure 52 shows an example of how the memory management operation codes
can be used to build a linked list of names.
*-----------------------------------------------------------------*
* Prototypes for subprocedures in this module *
*-----------------------------------------------------------------*
D AddName PR
D name_parm 40A
D Display PR
D Free PR
*-----------------------------------------------------------------*
* Each element in the list contains a pointer to the *
* name and a pointer to the next element *
*-----------------------------------------------------------------*
D elem DS BASED(elem@)
D name@ *
D next@ *
D name_len 5U 0
D nameVal S 40A BASED(name@)
D elemSize C %SIZE(elem)
*-----------------------------------------------------------------*
* The first element in the list is in static storage. *
* The name field of this element is not set to a value. *
*-----------------------------------------------------------------*
D first DS
D * INZ(*NULL)
D * INZ(*NULL)
D 5U 0 INZ(0)
*-----------------------------------------------------------------*
* This is the pointer to the current element. *
* When elem@ is set to the address of <first>, the list is *
* empty. *
*-----------------------------------------------------------------*
D elem@ S * INZ(%ADDR(first))
*-----------------------------------------------------------------*
* Put 5 elements in the list *
*-----------------------------------------------------------------*
C DO 5
C 'Name?' DSPLY name 40
C CALLP AddName(name)
C ENDDO
*-----------------------------------------------------------------*
* Display the list and then free it. *
*-----------------------------------------------------------------*
C CALLP Display
C CALLP Free
C EVAL *INLR = '1'
Figure 52 (Part 1 of 5). Memory Management - Build a Linked List of Names
114 ILE RPG for AS/400 Programmer's Guide