• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Automatic or Static Storage?

    August 27, 2008 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

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Midrange Dynamics North America

    With MDRapid, you can drastically reduce application downtime from hours to minutes. Deploying database changes quickly, even for multi-million and multi-billion record files, MDRapid is easy to integrate into day-to-day operations, allowing change and innovation to be continuous while reducing major business risks.

    Learn more.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    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

    Moshi Moshi Saga Continues at Bytware A Flash in the Pan or the Future of Data Storage?

    Leave a Reply Cancel reply

Volume 8, Number 30 -- August 27, 2008
THIS ISSUE SPONSORED BY:

Help/Systems
ProData Computer Services
System i Developer

Table of Contents

  • Automatic or Static Storage?
  • Jetty: An Efficient, Easy to Manage Alternative to WebSphere
  • Admin Alert: Giving Auditors What They Want

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23
  • SEU’s Fate, An IBM i V8, And The Odds Of A Power13
  • Tandberg Bankruptcy Leaves A Hole In IBM Power Storage
  • RPG Code Generation And The Agentic Future Of IBM i
  • A Bunch Of IBM i-Power Systems Things To Be Aware Of
  • IBM i PTF Guide, Volume 27, Numbers 21 And 22

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle