[Original article address: http://www.think400.dk/adhoc_4.htm%eks0015]
Problem:
Using VB or C, I can easily increase the array size without losing the original data. Is it possible in RPG?
Answer:
Yes, but you have to create an Array Based on the pointer. And increase the memory allocation size. Take a look at the operation code alloc, realloc, and dealloc.
The following is a quick example:
* Array definition-pointer-based
Darray s 10 dim (20000) based (PTR)
Dindex S 7 0
* Memory allocation data items -- I don't know how to translate this. According to my understanding, it is a pointer structure.
* Stores array information, such as the serial number, memory size, and array content in the array.
Dptr S *
Dnbr_of_elems S 5 0 INZ (10)
Dmem_size S 7 0 INZ
DX s 10i 0
* Allocate presentation memory =
* (Initial number of elements * size of the array content)
C eval mem_size = % size (array) * nbr_of_elems
C alloc mem_size PTR
C eval x = % ELEM (array)
* Cyclic testing
C 1 do 50 Index
* When the index exceeds the upper limit of the current array element
C If index> nbr_of_elems
* Add a step of 10.
* Multiply the size of the array element to get a new memory size.
C eval nbr_of_elems = nbr_of_elems + 10
C eval mem_size = % size (array) * nbr_of_elems
* Re-allocate memory blocks and increase the size.
C realloc mem_size PTR
C endif
* Move data test
C move Index Array (INDEX)
*
C enddo
* Release memory
C dealloc PTR
C eval * inlr = * on
Thanks to Mark D. Walter