David;
What Simon said plus in the called program/procedure I validate that the length of the expected format is less than or equal to the length passed in. I've never had a problem in production but I have created a new procedure with a longer format (Data Structure) and had this check fail because I did not change the caller to allocate enough storage for the new format.
Personally I have evolved from the pointer, length, format interface, to just a pointer interface e.g..
D procedure PR *
D structureBase * Value
Each data structure (format) has a preamble defined (static) e.g..
D formatxyz_t DS Qualified
D Template
D static LikeDs(static_t)
D data LikeDs(formatxyzData_t)
D formatXyz C Const('FORMATXYZ')
D static_t DS Qualified
D Template
D available 10I 0 Overlay(static) // Available (Allocated) bytes
D format 15A Overlay(static : *NEXT) // Name of the structure (Format)
The static part of the structure replaces the length, and format parms. Passing structures this way can be a little more dangerous, it is less intuitive and you have to make sure you have allocated enough storage for the static portion of the structure. But it allows much more flexibility IMO.
You can then take it a step further, and I suspect I'll get smacked for this, in the example prototype I pass a pointer to the structure by Value, in the caller I can then reallocate the pointed to storage making it larger/smaller as needed (ensure not to make it smaller than the static portion of the structure) and record the size of the reallocated storage in the available variable. Then when the procedure is done it returns the pointer to the reallocated storage. The caller would look like this:
D myPtr *
D myDs LikeDs(formatxyz_t)
D Based(myPtr)
myPtr = %Alloc(%Size(myDs.Static));
myDs.Static.Available = %Size(myDs.Static);
myDs.Static.Format = formatXyz;
myPtr = procedure(myPtr);
...
Reallocating storage in a called procedure is not recommended programming practice, you can easily shoot yourself in the foot, but in some cases the benefit outweighs the risk.
Duane Christen
--
Duane Christen
Senior Software Engineer
(319) 790-7162
Duane.Christen@xxxxxxxxxx
Visit PAETEC.COM
-----Original Message-----
From: rpg400-l-bounces@xxxxxxxxxxxx [mailto:rpg400-l-bounces@xxxxxxxxxxxx] On Behalf Of Simon Coulter
Sent: Wednesday, September 16, 2009 3:24 AM
To: RPG programming on the IBM i / System i
Subject: Re: OPTIONS(*OMIT)?
On 16/09/2009, at 4:27 PM, David FOXWELL wrote:
In your example, what is the importance of passing dsSize and dsFormat
?
Because only the address of the 'thing' (dsBase) is being passed.
Therefore you only know where it is. You don't know how long is or its layout. You could simply write the receiving program to expect a specific data structure but that's less flexible.
Duane's interface is much like the IBM API interface where the caller can instruct the receiver on how much data to return (dsSize) and in what format that data should be provided (dsFormat). This also allows the receiving program to be modified to return different or additional information without requiring the callers to be modified.
Regards,
Simon Coulter.
--------------------------------------------------------------------
FlyByNight Software OS/400, i5/OS Technical Specialists
http://www.flybynight.com.au/
Phone: +61 2 6657 8251 Mobile: +61 0411 091 400 /"\
Fax: +61 2 6657 8251 \ /
X
ASCII Ribbon campaign against HTML E-Mail / \
--------------------------------------------------------------------
--
This is the RPG programming on the IBM i / System i (RPG400-L) mailing list To post a message email: RPG400-L@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
visit:
http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives at
http://archive.midrange.com/rpg400-l.
As an Amazon Associate we earn from qualifying purchases.