|
Steve Richter wrote:
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"?
Rarely the case in practice.
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 )
As a seasoned C programmer, I know how often the C compiler complains to *me* about pointer type mismatches. It's great to get these diagnostics since it means that the error won't have to be debugged at run-time. As you said, since RPG pointers are "type-less", the RPG programmer has to wait until run-time to discover pointer type problems (or worse, NOT discover the problems!). Enhancements to the RPG language would be nice, but that still doesn't mean a programmer has to use them.
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
You want data pointers? Program in MI. But in practice, over the past 20 years, I can only think of one place where I've ever seen data pointers put to use.
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.
Or worse, using a pointer *after* the memory has been DEALLOC'ed. (If you don't DEALLOC, the memory will still be freed anyways when the AG ends.)
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 ).
Alternatively, you can just save yourself all the trouble of pointers and use alternatives. BTW, ever notice how pointers are *not* a feature of many modern programming languages? Not just Java, but also Perl, Python, Ruby, etc. (Oh, to be clear, pointers can be useful when using system API's. But that falls into the realm of systems programming, for which C is a more approriate language.) Cheers! Hans
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.