Home > AS/400 Tips > iSeries administrator tips > Dynamic memory in V5: Harness the power -- part 2
iSeries 400 Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ISERIES ADMINISTRATOR TIPS

Dynamic memory in V5: Harness the power -- part 2


Ron Turull
08.01.2006
Rating: -3.90- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Report Program Generator (RPG) programmers have been able to use dynamic memory (or "heap storage" as IBM likes to call it) for a number of years now, but not until V5 hit the streets had it been fully implemented. In the Dynamic memory in V5: Harness the power -- part 1, we discussed the evolution of dynamic memory on the iSeries (AS/400). We also discussed the other types of memory (i.e., static and automatic) and how they differ from dynamic memory. Now, we'll take a look at RPG's dynamic memory management built-in functions, and we'll see them in action.

More Information

RPG's easy-to-use dynamic memory management built-in functions and op-codes

There are three basic, easy-to-use heap APIs. There are three basic, even easier-to-use RPG heap op-codes. And with V5 and free-format RPG, these op-codes now have still even easier-to-use free-format versions. They are:

1. %Alloc (original op-code ALLOC, original API CEEGTST)
The Allocate Storage built-in function allocates the specified number of bytes from dynamic memory, returning a pointer to the first byte. The contents of the allocated memory are undetermined; your program is responsible for initializing it if needed. Note, the memory is guaranteed to be contiguous. The syntax is: %Alloc(NbrBytes)

2. %Realloc (original op-code REALLOC, original API CEECZST)
The Reallocate Storage with New Length built-in function resizes a block of dynamic memory previously allocated by the %Alloc built-in function or op-code. The block of memory is resized without changing the contents of the original block. The new size you specify can be larger or smaller. The op-code returns a pointer to the first byte of the resized storage block, which may be in a different location if the system has to move the block of memory to accommodate a larger size. While the contents of the original storage block remain unchanged (even if moved -- the original block is copied to the new location), the contents of any additional storage is undetermined. Again, the memory is guaranteed to be contiguous. And you can resize the storage as many times as needed. The syntax is: %Realloc(ptr : NbrBytes)

3. Dealloc (original op-code DEALLOC, original API CEEFRST)
The Free Storage op-code (notice that this one is not a built-in function -- it is a free-format op-code) frees, or deallocates, a block of dynamic memory previously allocated by either the %Alloc or %Realloc built-in functions or their op-codes counterparts. Dealloc did not get a built-in function version in V5; it got a free-format version of the op-code (probably because it doesn't have a return value). You can now use the Dealloc op-code in free-format code. The syntax is: Dealloc ptr

How to use the dynamic memory built-in functions and op-codes

The program HEAPFREE.RPG illustrates the use of RPG's heap built-in functions and op-codes. In quick summary, it first allocates storage for an array of 100 elements. Then it dynamically grows the array to 200 elements. Finally, it frees the storage allocated for the array.

The key to using these built-in functions and op-codes, and to accessing dynamic memory, is pointers -- basing pointers to be exact. In Integrated Language Environment (ILE) RPG, the variables used to access dynamic memory must be based on a pointer.

Examine the program at your leisure. We'll discuss it in depth in the next installment.

-----------------------------------
About the author: Ron Turull is editor of Inside Version 5. He has more than 20 years' experience programming for and managing AS/400-iSeries systems.


Rate this Tip
To rate tips, you must be a member of Search400.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
iSeries systems management
IBM System i gets VoIP via Nortel
SOA governance gets high rating from Dun & Bradstreet
The power of Power.org
Mainsoft enables .Net applications to run on WebSphere Portal
Determining the speed of an Ethernet card
IBM puts SOA-enabled portals on the iSeries, zSeries
Perils and pluses of a single-vendor shop
Introduction to i5 eServer Hardware Management Console (HMC)
Using Real-Time Business Data to Streamline Sarbanes-Oxley Compliance
Regulatory Compliance-The stakes are high!

iSeries administrator tips
Linux for IBM i: Introducing Linux to IBM i people
Application modernization for the iSeries: Why bother?
Application modernization in the i world
Checking on System i disk space requires creating a new command: XRTVSYSSTS
High availability planning and testing for iSeries: Notes, gloats, and moats
Viewing netstat information
Step-by-step PTF download determination
Preventing adopted special privileges on i5/OS
Display documents on your iSeries -- quick and easy
System i modularization: How-to tips -- part 4

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.

HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersProducts
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 1999 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts