dick@cs.vu.nl (Dick Grune) (05/15/88)
On the subject of programming style (a constant worry of mine), I should like to ask two (related) questions to the general programming public. When handling arrays, the bounds have to be described in some way. I know of three (consistent) methods to do so: 1. Address of first item and length. This is clean and can handle zero-length arrays. It gives, however, rise to incessant additions, since one wants the upper limit much more often than the length: if (p > array + length) error(); (or is it if (p >= array + length) error(); ?) 2. Address of first item and address of last item. This seems natural but you get +1 and -1 all over the place; I can hardly ever get this right and I have just given up correcting a piece of code someone else wrote in this style. 3. Address of first item and address of first non-item. This is unnatural and does not fit with everyday language, but it works. My code gets simpler (duller if you will), no more funny adjustments, the length of the array is the difference of the bounds, etc. The only problem with it is that there does not seem to be natural word or identifier for the first non-item (= the item after the last one). So my questions are: I. What do you use normally? and II. If you use method 3, what do you call the item past the last item? I will summarize to the net. (If it isn't clear yet, I use method 3 and clumsy names for the last plus one element). Dick Grune Vrije Universiteit de Boelelaan 1081 1081 HV Amsterdam the Netherlands dick@cs.vu.nl ...!mcvax!vu44!dick
franka@mmintl.UUCP (Frank Adams) (05/17/88)
In article <724@dick.cs.vu.nl> dick@cs.vu.nl (Dick Grune) writes: >1. Address of first item and length. >This is clean and can handle zero-length arrays. It gives, however, rise to >incessant additions, since one wants the upper limit much more often than the >length: > if (p >= array + length) error(); Note that this is a function of the programming language in use. In C, where arrays are most often referenced by explicit pointer arithmetic, this is true. But in most programming languages, arrays are referenced by indexing (as, indeed, they can be in C). And, in this case, the length is exactly what you need. -- Frank Adams ihnp4!philabs!pwa-b!mmintl!franka Ashton-Tate 52 Oakland Ave North E. Hartford, CT 06108
bill@proxftl.UUCP (T. William Wells) (05/18/88)
I tried to e-mail my response but that failed; readnews gave me a memory fault. Then I tried mail but I was unable to get it to acknowledge the address. So, could you give me an idea of the address to use with uucp? (E-mail to proxftl!bill please, assuming this is possible.) Anyway, here is my message: Methods 1 and 3 are equivalent: the first unused element IS the first element + length. (B.T.W: use if (p >= array + length) error(); instead of if (p > array + length) error(); ) I use both, depending on circumstances. Actually, when I use method 1, I write it: if (p >= &array[length]) error(); which is also method 3 except that the address is computed right then. An example would be: #define ARRAYLEN 1024 /* How long is my array? */ int Array[ARRAYLEN]; === if (p >= &Array[ARRAYLEN]) error(); Of course, if the array is dynamically allocated or on the stack, the end address computation has a penalty. If that penalty is too large, I do compute a pointer; its name is something like `array_end'. A word on terminology: in order to eliminate confusion, I consistently refer to the final element of an array as ...last or something to that effect; I refer to the place just after that element as ...end.