• 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
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    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

  • Liam Allan Shares What’s Coming Next With Code For IBM i
  • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
  • VS Code Will Be The Heart Of The Modern IBM i Platform
  • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
  • IBM i PTF Guide, Volume 27, Number 25
  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24

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