× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



How do I declare an array with a dynamic number of elements?

In RPG IV, the new (V3 R7) ALLOC, REALLOC and DEALLOC operation codes can be
used to allocate memory. This means that at run time, you can go out to the
system and ask it to assign storage to the program that was not allocated to
the program when it was evoked.
These operation codes can be used to allocate memory up to 16MB. The
allocation can be assigned to a pointer variable. In RPG IV, pointers have
the data-type of asterisk (*). All that is needed is to allocate memory to a
pointer that is used with the BASED keyword of the desired dynamic array.
The example that follows illustrates this technique:
.....DName+++++++++++EUDS.......Length+TDc.Functions++++++++++++++++++

     D DynoArr         S              7P 0 Dim(10000) based( pDynoArr)

     D nSize           S             10i 0



.....CSRn01..............OpCode(ex)Extended-factor2++++++++++++++++++

     C                   Eval      nsize = %size(DinoArr) * 64

.....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq

     C                   Alloc     nSize         pDynoArr

     **  We now have enough storage allocated for 64 elements.



     C                   Eval      nsize = %size(DinoArr) * 70

     C                   ReAlloc   nSize         pDynoArr

     **  We have changed the allocation to enough storage for 70 elements

     C* ... code to use the array goes here...

     C                   Dealloc(N)              pDynoArr

     **  We have just returned the storage to the system. 

To increase or decrease the number of elements in the dynamic array, use the
REALLOC operation code. Simply change the number of bytes that need to be
allocated, and call REALLOC with the new size in Factor 2 and the original
pointer variable in the Result field. REALLOC allocates new storage of the
size specified, and copies the original data to that new location. Then it
frees ("deallocates") the original storage.

IMPORTANT: Always remember to DEALLOC anything you ALLOC. That is always
free up memory that you have allocated otherwise memory leaks will be
created.

If you are not on V3 R7, you can still use dynamic memory by calling one of
the system APIs or linking into the QC2LE binding directory and calling the
C runtime MALLOC and DEALLOC functions.

 
 
Thx,
wdd

-----Original Message-----
From: rpgnext-bounces@xxxxxxxxxxxx [mailto:rpgnext-bounces@xxxxxxxxxxxx] On
Behalf Of Joel Cochran
Sent: Wednesday, January 25, 2006 2:42 PM
To: RPGNext Discussion and Information
Subject: Re: [RPGNext] I checked in the MAKESOURCE member and here is what
Ihave found...




On 1/25/06, David Daugherty <ddaugherty@xxxxxxxxxxx> wrote: 

Joel:
 
I checked in the MAKESOURCE member and here is what I have found:
 
0006.55 d fieldList       s          32000a   varying              
0006.56 d fieldArray      s            100a   dim(999) varying     
0006.57 d fieldCount      s             10i 0 inz                  
 
Am I checking in the correct place?
 
Thx,
wdd


Hey David,

No, in looking, the limitation is actually in the RPGNEXT QUSLFLD wrapper.
I have it set to 256 fields, primarily because it is the number of
occurances for a large MODS.  To correct it would mean creating a ton of
empty datastructures and may negatively impact performance.  Unfortunately,
the RPGNEXT code is not open source, so you can't see it for me.  Would you
be willing to test it if I sent you an updated version? 


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.