|
P dumbProcDesign B D PI * D data S 100 D ptr S * INZ(%ADDR(data)) C RETURN ptr P E P badProcDesign B D PI * D data S 100 STATIC D ptr S * INZ(%ADDR(data)) C RETURN ptr P E P betterProcDesign B D PI * D ptr S * C ALLOC 100 ptr C RETURN ptr P E P bestProcDesign B D PI 5I 0 D ptrArray S * STATIC DIM(100) D i S 5I 0 STATIC INZ(0) C EVAL i = i + 1 C ALLOC 100 ptr(i) C RETURN i P E Explanation: dumbProcDesign is dumb because it returns a pointer to automatic storage which no longer exists after the procedure returns. badProcDesign is bad because it uses STATIC to overcome the dumb choice above thus all callers get addressability to the same storage -- probably not the intent. If that is the intent then ptr should be STATIC also. betterProcDesign is better because by allocating storage from the heap the storage can persist beyond the life of the allocating procedure. There are still flaws in this design in that the caller must remember to release the allocated storage and also by returning a pointer you allow the caller to mess directly with the internals of what the pointer is referencing. bestProcDesign is the best of these designs because it exposes no information about the internal design. It returns only a handle (in the true sense) which can be used by subsequent functions. This approach generally requires a series of support methods to accept the handle and do something with it. For instance: aHandle getXxxHandle() rlsXxxHandle( aHandle ) doStuff( aHandle, p1, p2, ... ) Of course, a production version would not use a fixed size pointer array, would probably make the pointer array global to the module, would map the allocated storage using a based data structure, and would wrap the whole collection of functions in a service program. Regards, Simon Coulter. -------------------------------------------------------------------- FlyByNight Software AS/400 Technical Specialists http://www.flybynight.com.au/ Phone: +61 3 9419 0175 Mobile: +61 0411 091 400 /"\ Fax: +61 3 9419 0175 mailto: shc@flybynight.com.au \ / X ASCII Ribbon campaign against HTML E-Mail / \ --------------------------------------------------------------------
As an Amazon Associate we earn from qualifying purchases.
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.