fhg
Volume 8, Number 30 -- August 27, 2008

Automatic or Static Storage?

Published: August 27, 2008

by Susan Gantner

If you write RPG subprocedures, you should know about the differences between automatic and static storage. (If you don't write subprocedures, shame on you!) I've found that many writers and/or users of subprocedures don't fully understand the differences. So let's start at the beginning.

By default, fields (i.e., stand-alone fields, data structures, arrays, etc.) defined inside a subprocedure (a.k.a. local fields) use automatic storage. This means that the storage doesn't exist until the procedure is called and it is cleaned up when the procedure returns to its caller. Of course, since the storage goes away between calls to the procedure, that means the values are reinitialized between each call to the procedure (and the status of the LR indicator has no impact.)

This differs from fields defined outside of subprocedures (i.e., in the main part of the program--or before the first P spec in the source member) that use static storage. Static storage exists from the time the program is called until the activation group where it is running ends, or until the job ends. Notice, though, that the values in static fields defined outside the P specs are re-initialized on return if the LR indicator was turned on; with LR off, the values remain the same between multiple calls to the program.

You have no choice for the type of storage used for the global fields, but for the local fields in your procedures, you do have the option of making them static by coding the STATIC keyword. This makes them behave almost the same as the global fields. They will retain their values between multiple calls to the procedure.

However, there is still a difference in behavior between local static fields in a procedure and global fields. The local static fields are not impacted by the status of the LR indicator, so they retain their values between calls to the program even if LR was set on before the return.

To see if you've got the idea, study the following code and then try to answer the following questions.

    H  DftActGrp(*NO)  ActGrp('QILE') Option(*SrcStmt)

     D ProcAuto        PR

     D I               S              5U 0
     D GlobalCount     S              3  0 Inz

      /FREE

       For I = 1 to 3;   // Call the subprocedure 3 times
         ProcAuto();
       EndFor;

       *INLR = *On;

      /END-FREE
     P ProcAuto        B
     D                 PI

     D AutoCount       S              3  0
     D StatCount       S              3  0 Static

      /FREE
       StatCount += 1;  
       AutoCount += 1;
       GlobalCount += 1;
       Dsply (' AutoCount=' + %Char(AutoCount) +
             ', StatCount=' + %Char(StatCount) +
             ', GlobalCount=' + %Char(GlobalCount));

      /END-FREE
     P ProcAuto        E

  1. On the first call in the job to this *PGM, what values will be displayed for AutoCount, StaticCount and GlobalCount on each of the three calls?
  2. After the program has run once, if you were to call it again right away in the same job, what values would you see for them on the second iteration of the program?
  3. If you issue a RCLACTGRP QILE (or sign off and back on) and call the program a third time, what values do I see?

If you answered the following for question 1...

AutoCount=1, StatCount=1, GlobalCount=1
AutoCount=1, StatCount=2, GlobalCount=2
AutoCount=1, StatCount=3, GlobalCount=3

...and the following for question 2...

AutoCount=1, StatCount=4, GlobalCount=1
AutoCount=1, StatCount=5, GlobalCount=2
AutoCount=1, StatCount=6, GlobalCount=3

...and the following for question 3...

AutoCount=1, StatCount=1, GlobalCount=1
AutoCount=1, StatCount=2, GlobalCount=2
AutoCount=1, StatCount=3, GlobalCount=3

Congratulations, you've got it!

Of course, all this begs the question of how to initialize or re-initialize static values in a procedure when you need to? The simple answer is to do it manually in the logic, with something like a CLEAR or EVAL operation.

But how do you signal the subprocedure when to re-initialize the static fields? Presumably since you made them static, they should not be initialized on every call. This requires a bit of thought and there are many potential solutions. Many people have a special parameter that is passed to tell the procedure to initialize. It might be a *NoPass parameter at the end of the list that is only used when initialization is required. Another popular option is to make all the parameters *NoPass and when the procedure is called with %Parms = 0, it is the signal to reinitialize.

There are many other options that could be used. The ones you use will likely be different for internal subprocedures vs. procedures in a Service Program. But the important thing to remember is that you'll need to accommodate this in your design if you need to use static fields in a subprocedure.


Susan Gantner is one of the most respected System i gurus in the world and is one of the co-founders of System i Developer, an organization dedicated to RPG, DB2, and other relevant software technologies for the System i platform. Gantner, who has worked in IBM's Rochester and Toronto labs, left IBM to focus on training OS/400 and i5/OS shops on the latest programming technologies. She is also a regular speaker at COMMON and other user groups. Send your questions or comments for Susan to Ted Holt via the IT Jungle Contact page.




                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


Sponsored By
HELP/SYSTEMS

SEQUEL™ is the leading business intelligence solution for the IBM® System i™.

                      · Executive Dashboards with Gauges for Critical Business Metrics
                      · Graphical Query and Reporting
                      · Drill-Down Data Analysis
                      · Multi-Platform Database Access
                      · Powerful Scripting with CL-like Programming
                      · Secure Web Access

SEQUEL meets all your System i data access and analysis requirements.

Learn more now.


Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Edwin Earley, Brian Kelly, Michael Sansoterra
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

Infinite Software:  Migrate System i RPG or COBOL apps to Linux, Windows or Unix
COMMON:  Join us at the Focus 2008 workshop conference, October 5 - 8, in San Francisco, California
Computer Measurement Group:  CMG '08 International Conference, December 7-12, Las Vegas


 

IT Jungle Store Top Book Picks

Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
Getting Started with PHP for i5/OS: List Price, $59.95
The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket Developers' Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
iSeries Express Web Implementer's Guide: List Price, $59.00
Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
WebFacing Application Design and Development Guide: List Price, $55.00
Can the AS/400 Survive IBM?: List Price, $49.00
The All-Everything Machine: List Price, $29.95
Chip Wars: List Price, $29.95


 
The Four Hundred
PowerTech Acquired by Help/Systems, Private Equity Firm

JDA Ponies Up $346 Million to Buy i2 Technologies

SMBs Are Sensibly More Concerned with Biz than Tech

As I See It: Lessons from Robben Island

Big Blue Launches XIV Clustered Storage Arrays

The Linux Beacon
Why Blade Servers Still Don't Cut It, and How They Might

Intel Keeps Both Arms Swinging with Xeons, Jabs with Itanium

Microsoft Ponies Up Another $100 Million for Novell Linux

Mad Dog 21/21: Newtonian Economics

Two More Xeon-Based Galaxy Servers from Sun

Four Hundred Stuff
A Bumblebee for BI--Now That's Just 'Smart'

Curbstone Gains PCI Compliance for i OS Payment System

Life is Easy for iPhone Apps on the Morph Labs Cloud

WebClient for CA Plex 1.4 Now Available

Avnet to Resell VDoc Content Management Suite in U.S.

Big Iron
For Some Customers, the Mainframe Is Green

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
August 23, 2008: Volume 10, Number 34

August 16, 2008: Volume 10, Number 33

August 9, 2008: Volume 10, Number 32

August 2, 2008: Volume 10, Number 31

July 26, 2008: Volume 10, Number 30

July 19, 2008: Volume 10, Number 29

The Windows Observer
Windows 7 Means Windows Server 2008 R2, Microsoft Reveals

Free Range Apps Can Roam the Farm, Microsoft Says

It's Black Tuesday for Microsoft, with 26 Flaws Patched

Why Blade Servers Still Don't Cut It, and How They Might

SQL Server 2008 Goes RTM

The Unix Guardian
What the Heck Is the Midrange, Anyway?

Overseas and Notebook Sales Offset Printer Declines for HP in Q3

Two More Xeon-Based Galaxy Servers from Sun

Mad Dog 21/21: Newtonian Economics

Intel's Nehalems to Star at IDF, AMD Pitches Shanghai

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

THIS ISSUE SPONSORED BY:

Help/Systems
ProData Computer Services
System i Developer


Printer Friendly Version


TABLE OF CONTENTS
Automatic or Static Storage?

Jetty: An Efficient, Easy to Manage Alternative to WebSphere

Admin Alert: Giving Auditors What They Want

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
MQ Help Desired

Printing TCP/IP Details into a Spoolfile

IFF ACTIVE Equivalent in CL

Capture Sort File and Copy to Database File

SNMP Traps on i5OS

Java Messages





 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement