With the release of V3R1, IBM made pointers available to ILE RPG programmers. Pointers are useful when writing calls to APIs; they can make programs faster, and they allow greater flexibility. IBM has failed, however, to fully implement pointer arithmetic. The only method available is to define an array based on a pointer. (See "Pointers in ILE RPG," MC, June 1995). With an upper limit of 32767 for array sizes, though, this technique limits the maximum offset possible. The program in 2 allows pointer offset calculations greater than 32767.
With the release of V3R1, IBM made pointers available to ILE RPG programmers. Pointers are useful when writing calls to APIs; they can make programs faster, and they allow greater flexibility. IBM has failed, however, to fully implement pointer arithmetic. The only method available is to define an array based on a pointer. (See "Pointers in ILE RPG," MC, June 1995). With an upper limit of 32767 for array sizes, though, this technique limits the maximum offset possible. The program in Figure 2 allows pointer offset calculations greater than 32767.
This program assumes that the calling program has already allocated a block of memory. These are the incoming parameters:
? SpcPtr?A pointer to the beginning of the block of memory.
? OffSet?A zoned decimal number containing the offset from SpcPtr. The offset must be positive; the program will only perform pointer addition.
? NewPtr?A pointer returned to the caller. It contains the result of adding OffSet to SpcPtr.
The program works by defining an array (Arr) of size 32767 based on the pointer WrkPtr. If the offset is greater than 32760, the program sets WrkPtr to the address of element 32761 of the array and decrements the temporary offset by 32760. The program does this until the temporary offset is less than 32760. (I use 32760 rather than 32766 to ensure that I don't run off the end of the array.) The variable NewPtr is set to the address of the array element subscripted by the temporary offset plus one. This technique effectively leapfrogs through the array until the offset address is located.
You can incorporate this program in a larger program or compile it separately as a module and bind it into other ILE programs.
? Simon Ritchie
TechTalk: ILE RPG Pointer Arithmetic
Figure 2: Pointer Offset Calculation
D WrkPtr S * D Arr S 1 BASED(WrkPtr) DIM(32767) D SpcPtr S * D NewPtr S * D OffSet S 15 0 D WrkOffSet S 15 0 * C *ENTRY PLIST C PARM SpcPtr C PARM OffSet C PARM NewPtr * * Set work variables C EVAL WrkPtr = SpcPtr C EVAL WrkOffSet = OffSet * * While the offset is greater than the size of the array, * set the based on array pointer to the end of the array * and decrement the offset by the same amount C DOW WrkOffSet > 32760 C EVAL WrkPtr = %ADDR(Arr(32761)) C EVAL WrkOffSet = WrkOffSet - 32760 C ENDDO * * When the new pointer is somewhere within the array, set * the new pointer to the address of the calculated offset C EVAL NewPtr = %ADDR(Arr(WrkOffSet +1)) * C EVAL *INLR = *ON
LATEST COMMENTS
MC Press Online