|
Eric, Pointers are nothing to be afraid of. ( regardless of what Hans says ) And why not use them when they allow your code to be more direct, easier to read, more to the "point"? There are problems, maybe better said as "potential sources of errors", when using pointers. The first PSE is that pointers are typeless, that is the compiler does not know whether the pointer points to a packed decimal, a character string or data structure. A good start in dealing with this problem is to allow a pointer to be declared with a type attribute: d pName s * Like( OrdHdr.CustName ) d pOrdHdr s * likeds( OrdHdr ) An additional way to reduce type mixup errors is to permit data pointers to be used in rpg. Data pointers were built into the original s/38 by some genius who was surely banished from Rochester long ago. A data pointer contains both a pointer to something and also the attributes of the something. This elimiates the type mixup and makes for even cleaner code. d Name s 30a d AnotherName s 30a d Amount s 7p 2 d dpSomething s * dataptr /free dpSomething = %AddrAndAttr( Name ) ; AnotherName = dpSomething ; // copies Name to AnotherName /end-free The 2nd source of error is not with the pointer itself, but the requirement to free the memory that has been allocated as the program runs. That is you must Dealloc what has been %alloc'd. The current RPG ILE is wide open for this kind of error. To deal with it you either go the java route and handle all alloc and dealloc internally. Or go the C++ route and implement real good scoping with constructor and destructor code that runs automatically on scope entry and exit ( procedure/program entry and exit ). Steve Richter
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.