Three kinds of memory to consider
From "Harnessing the power of RPG's dynamic memory op-codes" by Ron Turull, AS/400 Experts Journal, Vol. 3, No. 2, March/April 2000. Provided courtesy of The 400 Group (http://www.the400group.com).
For years dynamic memory support has given C programmers the ability to dynamically (i.e., during run-time) build chains of data structures and grow and shrink arrays. Since V3R2, ILE RPG and ILE COBOL programmers have been able to take advantage of dynamic memory using the dynamic memory (or heap storage management) APIs. Now, RPG has intrinsic op-codes that allow the RPGer to more readily access dynamic memory.
On most computers, there are three common kinds of memory available for use by programs and their variables:
The main difference is the way the memory is allocated, deallocated (or freed) and initialized. It is important to remember that these different types of memory are distinguished logically, not physically. They also give rise to three different types of variables:
1. Static variables
2. Automatic variables
3. Dynamic variables
The memory for every static variable declared in your program is allocated at program initialization. Currently in RPG, all variables declared in the main source section are static variables. (Variables declared in the subprocedures are automatic variables.) 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 (i.e., when the program returns with LR set on).
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 (that is, put on the call stack) and it goes out of scope when the procedure returns to its caller (that is, taken off the call stack). In RPG, variables declared in the subprocedures are automatic variables by default, but they 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 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.
RPG's op-codes are easier than the APIs.
There are three basic, easy-to-use heap APIs. There are three basic, even easier-to-use RPG heap op-codes. They are (the corresponding APIs are shown in parenthesis):
1. Alloc (CEEGTST). The Allocate Storage op-code 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.
2. Realloc (CEECZST). The Reallocate Storage with New Length op-code resizes a block of dynamic memory previously allocated by the Alloc 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 black, 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, the contents of any additional storage is undetermined. Again, the memory is guaranteed to be contiguous.
3. Dealloc (CEEFRST). The Free Storage op-code frees, or deallocates, a block of dynamic memory previously allocated by either the Alloc or Realloc op-code.