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

Ron Turull
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. With RPG's dynamic memory built-in functions, working with dynamic memory is easier and more intuitive than ever. This is powerful stuff -- it puts you in charge! It is also necessary in order to use many of the new technologies IBM has put into the iSeries (AS/400), for example, sockets, multi-threading and Java integration.

More Information

The evolution of dynamic memory

For years, the C language's dynamic memory support gave C programmers the ability to, for example, dynamically (i.e., during run-time) build chains of data structures and grow and shrink arrays at will. Since V3R2, Integrated Language Environment (ILE) RPG and ILE COBOL programmers have also been able to take advantage of dynamic memory, using the ILE dynamic memory APIs. But even though these APIs are relatively simple, they are still complicated by extraneous parameters that could have been left off or left to another API.

A couple of years later, in V4R2, came the introduction of the dynamic memory operation codes in RPG. This greatly simplified using dynamic memory, as the RPG op-codes were much more intuitive than the APIs.

Finally, with V5, the implementation of the dynamic memory support in RPG has reached an apex. In addition to the fixed-format op-codes, RPG now supports two dynamic memory built-in functions, %Alloc and %Realloc, and the free-format op-code Dealloc. The relatively new built-in functions make the process of managing dynamic memory a breeze.

Understanding the three kinds of memory

On most computers, there are three common kinds of memory available for use by programs and their variables:

1. Static

2. Automatic

3. Dynamic

The main difference among these three is the way the memory is allocated, initialized, and de-allocated (or freed). It is important to remember that these different types of memory are distinguished logically, not necessarily physically; that is, while they might be located in different storage blocks, they are all kept in the main computer memory (i.e., the RAM).

These three different kinds of memory give rise to three different types of variables:

  • Static variables

  • Automatic variables

  • Dynamic variables

    The memory for every static variable declared in your program is allocated at program initialization (a.k.a. program activation). Currently in RPG, all variables declared in the main source section are static variables (as opposed to variables declared in subprocedures -- or non-main procedures -- which are automatic variables by default). Static variables are initialized when they are allocated; character variables are initialized to blanks and numeric variables are initialized to zero (pointers are initialized to null). Static memory allocated by a program is freed automatically when the program is unloaded (e.g., when an RPG program returns with LR set on -- this is called program deactivation ).

    Memory for automatic variables, sometimes referred to as stack variables, is allocated when an automatic variable comes into scope and is freed when the variable goes out of scope. An automatic variable comes into scope when the procedure in which it is declared is called (i.e., put on the call stack) and it goes out of scope when the procedure returns to its caller (i.e., taken off the call stack). In RPG, variables declared in subprocedures are automatic variables by default, but can be declared as static if you use the static keyword on the associated D-spec.

    The memory for a dynamic variable is allocated and freed manually by your program. For example, if your program needs memory for an array of 100 elements, each two characters long, it would request 200 bytes of dynamic memory. Later, if the program needs to expand the array to 200 elements, it can request that the original 200-byte allocation be increased to 400 bytes.

    Memory for dynamic variables is allocated off an internal memory structure called the heap. The heap is simply a chunk of memory that the system allocates for an activation group when the activation group is created. Any program in the activation group can request storage off that activation group's heap (in a coming installment, we'll discuss 10 important things to remember about heaps).

    We'll discuss RPG's dynamic memory management built-in functions -- and see them in action -- 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.

    This was first published in July 2006

  • There are Comments. Add yours.

    TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

    REGISTER or login:

    Forgot Password?
    By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
    Sort by: OldestNewest

    Forgot Password?

    No problem! Submit your e-mail address below. We'll send you an email containing your password.

    Your password has been sent to:

    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.