× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Kim Spears wrote:
To fellow ILEers

I'm trying to use a pointer to a data structure to pass data back from a
sub procedure. I had it working at one point but have since made some
changes and now it doesn't work. What's happening is $obcGetType loads
the data structure properly and the pointer is getting passed back to
$obcSelect, however the data in the 'type' data structure in $obSelect
is garbage. It's almost like once $obcGetType goes out of scope the
memory for type_ds gets corrupted. Any suggestions?
You exactly nailed it. type_ds is "automatic" storage, meaning it is allocated when $obcGetType is called and released when $obcGetType returns. Once it returns, you have no idea what is occupying that memory space. You must never return a pointer to your local storage to an outside routine.

There are a few ways to get around this.

One is to allocate memory explicitly in your called routine. The problem with that is that you have to remember to free the memory when you're done with it. This is whee you get memory leaks.

Another option is to make the variable STATIC. This means the variable will be placed in the general program storage when the program is called, as opposed to being allocated when the routine is called. A cool thing about this is that you can use it to keep information between calls. For example, say you have a method GetCustomer that takes a customer number as the input. Well, with static storage you could compare the customer number to the number in the data structure and not do the CHAIN if the record is already there. The downside to this method is that it doesn't re-initialize the variables on every call (although you can do that reasonably easily with the RESET opcode) and also you can run into some issues with multi-threading, although I'm not 100% clear on the details in RPG, expecially in V6R1.

Yet another way is to define the data structure in the calling procedure and pass the address in to the called subprocedure. I do this all the time.

Joe

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.