TechTalk: ILE RPG Pointer Arithmetic

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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 
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: