How to dynamically increase the size of an array

Source: Internet
Author: User
Tags array definition

[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

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.