|
Colin, Here is a generic program I use as a template when I'm building applications with dynamic arrays. FYI: When a ReAlloc is performed, "new" storage on the heap is allocated. The "new" storage is initialized by the "old" storage. The "old" storage is then released. The final amount of storage used is released with a DeAlloc or when the activation group dies. I hope it is what your looking for. I'd be glad to help if you have any questions. Dave 0000.01 *#########################################* 0000.02 * Constants * 0000.03 *#########################################* 0042.15 0042.18 * The size of an element in the array. 0042.19 D ArrayElmSize C %Size(ArrayElm) 0042.20 0042.21 * The # of elements by which the array will be incremented 0042.22 * each time memory is allocated. 0042.23 D ArrayIncAmt C 5 0042.24 0042.25 *############################* 0042.26 * Data Structures * 0042.27 *############################* 0042.29 0042.30 *#######################################################* 0042.31 * Because the array is based, the compiler reserves no space * 0042.32 * for it at all and assumes you know what you are doing with * 0042.33 * the pointer ArrayPtr. * 0042.34 * If a variable is based, it means that it doesn't have any memory * 0042.35 * dedicated to it - it isn't fixed at a specific address. * 0042.36 * It will be at wherever its 'basing pointer' is set to. * 0042.37 *#######################################################* 0000.01 *##########################################* 0000.02 * Constants * 0000.03 *##########################################* 0042.15 0042.18 * The size of an element in the array. 0042.19 D ArrayElmSize C %Size(ArrayElm) 0042.20 0042.21 * The # of elements by which the array will be incremented 0042.22 * each time memory is allocated. 0042.23 D ArrayIncAmt C 5 0042.24 0042.25 *###########################################* 0042.26 * Data Structures * 0042.27 *###########################################* 0042.29 0042.30 *#######################################################* 0042.31 * Because the array is based, the compiler reserves no space * 0042.32 * for it at all and assumes you know what you are doing with * 0042.33 * the pointer ArrayPtr. * 0042.34 * If a variable is based, it means that it doesn't have any memory * 0042.35 * dedicated to it - it isn't fixed at a specific address. * 0042.36 * It will be at wherever its 'basing pointer' is set to. * 0042.37 *######################################################* 0042.38 D ArrayElm DS 0042.40 D Name 25 0042.41 D Address 25 0042.42 0042.43 *############################################* 0042.44 * StandAlone Fields * 0042.45 *############################################* 0042.46 0042.47 D Array S Like(ArrayElm) Dim(32767) 0042.48 D Based(ArrayPtr) 0042.49 0042.52 * The current # of elements in the array. 0042.53 D Array#ofElm S 10 0 0042.54 0042.56 * The array index. 0042.57 D ArrayIndex S 5 0 0042.58 0042.59 * The index of the last element of the array. 0042.60 D ArrayLastElm S 5 0 0042.61 0042.62 * The current # of elements in the array. 0042.63 D ArrayPtr S * 0042.64 0042.65 * The current # of bytes of storage allocated to the array. 0042.66 D ArrayStorage S 10 0 0042.67 0059.03 0059.04 *##############################################* 0059.05 * Allocate initial storage for the array. * 0059.06 *##############################################* 0059.07 C Eval ArrayLastElm = 0 0059.08 C Eval ArrayPtr = *Null 0059.09 C Eval Array#ofElm = ArrayIncAmt 0059.10 C Eval ArrayStorage = Array#ofElm * ArrayElmSize 0059.11 C Alloc ArrayStorage ArrayPtr 0059.12 0059.19 C Do 32767 0059.20 0059.21 C If ArrayLastElm = Array#ofElm 0059.22 C Eval Array#ofElm = Array#ofElm + ArrayIncAmt 0059.23 C Eval ArrayStorage = Array#ofElm * ArrayElmSize 0059.24 C ReAlloc ArrayStorage ArrayPtr 0059.26 C EndIf 0059.28 0059.29 C Eval ArrayLastElm = ArrayLastElm + 1 0059.30 C Eval Array(ArrayLastElm) = 'Who Cares' 0059.31 C Eval %Subst(Array(ArrayLastElm):25) = '123 Main Street' 0059.35 C EndDo 0059.36 0097.00 C Eval *INLR = *ON 0097.01 ****************** End of data **************************************** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * This is the RPG/400 Discussion Mailing List! To submit a new * * message, send your mail to "RPG400-L@midrange.com". To unsubscribe * * from this list send email to MAJORDOMO@midrange.com and specify * * 'unsubscribe RPG400-L' in the body of your message. Questions should * * be directed to the list owner / operator: david@midrange.com * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
As an Amazon Associate we earn from qualifying purchases.
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.