• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • The Proper Way To Deallocate A Pointer

    May 9, 2012 Ted Holt

    Jerry Clower used to tell a story about attending a rattlesnake roundup. He said that people from the humane society were on hand to be sure that the rattlesnakes were killed properly. Mr. Clower remarked that he didn’t know that there was an improper way to kill a rattlesnake. Pointers can be like rattlesnakes, biting you when you don’t expect it. Do you know the proper way to kill a pointer?

    Use the %ALLOC function to allocate memory to a program. The system copies the address of the memory into a pointer variable.

    D SomePointer     s
    D SomeData        s            256a   based(SomePointer)
    
     /free
         SomePointer = %alloc(%size(SomeData));
    

    Now you can store a value in the allocated memory, just as you store values in variables that you define in a program.

    SomeData = 'Rufus T. Firefly';
    

    And when you’re finished with the memory, you can tell the operating system you no longer need it.

    dealloc SomePtr;
    

    However, if you’re not careful, you may inadvertently use the memory that no longer belongs to you, because the pointer still has the value the %ALLOC function assigned to it. The fact that the pointer still has a non-null value could cause big problems, because the system will still allow you to assign values to the based variable, even though that memory is no longer allocated to the program.

    The solution is to use the N extender when you deallocate the memory.

    dealloc(n) SomePtr;
    

    The N extender sets the pointer to null. Now if you try to use the pointer, you will get error MCH3601 (Pointer not set for location referenced.) Using the N extender will let you use code like the following to keep your pointer operations from causing damage.

         if SomePointer <> *null;
            . . . do whatever . . .
         else;
            . . . take care of the error . . .
         endif;
    

    And that is the proper way to kill a pointer.



                         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
    VISUAL LANSA 16 WEBINAR

    Trying to balance stability and agility in your IBM i environment?

    Join this webinar and explore Visual LANSA 16 – our enhanced professional low-code platform designed to help organizations running on IBM i evolve seamlessly for what’s next.

    🎙️VISUAL LANSA 16 WEBINAR

    Break Monolithic IBM i Applications and Unlock New Value

    Explore modernization without rewriting. Decouple monolithic applications and extend their value through integration with modern services, web frameworks, and cloud technologies.

    🗓️ July 10, 2025

    ⏰ 9 AM – 10 AM CDT (4 PM to 5 PM CEST)

    See the webinar schedule in your time zone

    Register to join the webinar now

    What to Expect

    • Get to know Visual LANSA 16, its core features, latest enhancements, and use cases
    • Understand how you can transition to a MACH-aligned architecture to enable faster innovation
    • Discover native REST APIs, WebView2 support, cloud-ready Azure licensing, and more to help transform and scale your IBM i applications

    Read more about V16 here.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    ProData Computer Services:  Save the day with RDR and ProData utilities! Download FREE Trial
    looksoftware:  Webcast - No Limits: The Power of IBM i. Download the video now!
    Four Hundred Monitor Calendar:  Latest info on national conferences, local events, & Webinars

    IT Jungle Store Top Book Picks

    BACK IN STOCK: Easy Steps to Internet Programming for System i: List Price, $49.95

    The iSeries Express Web Implementer's Guide: List Price, $49.95
    The iSeries Pocket Database Guide: List Price, $59
    The iSeries Pocket SQL Guide: List Price, $59
    The iSeries Pocket WebFacing Primer: List Price, $39
    Migrating to WebSphere Express for iSeries: List Price, $49
    Getting Started with WebSphere Express for iSeries: List Price, $49
    The All-Everything Operating System: List Price, $35
    The Best Joomla! Tutorial Ever!: List Price, $19.95

    Safestone Updates Cross-Platform Compliance Tool COMMON Finds Its Happy Spot With IBM i And Disneyland

    Leave a Reply Cancel reply

Volume 12, Number 13 -- May 9, 2012
THIS ISSUE SPONSORED BY:

SEQUEL Software
Help/Systems
CNX

Table of Contents

  • OLAP Is Simple, Once You Understand It
  • The Proper Way To Deallocate A Pointer
  • Admin Alert: Prepping For And Responding To An Unheard Of IBM i #FAIL

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