Xref: utzoo comp.lang.modula2:673 comp.lang.misc:1131 Path: utzoo!mnetor!uunet!lll-winken!lll-lcc!mordor!sri-spam!sri-unix!quintus!pds From: pds@quintus.UUCP (Peter Schachte) Newsgroups: comp.lang.modula2,comp.lang.misc Subject: Re: 0-based/1-based arrays Message-ID: <726@sandino.quintus.UUCP> Date: 3 Mar 88 19:23:47 GMT References: <7161@sol.ARPA> <2740@mmintl.UUCP> <4343@june.cs.washington.edu> <2851@haddock.ISC.COM> Organization: Quintus Computer Systems, Mountain View, CA Lines: 28 Summary: How is that any different than for 1-based arrays? In article <2851@haddock.ISC.COM>, karl@haddock.ISC.COM (Karl Heuer) writes: > When dealing with 0-based arrays, the appropriate concept is a half-open > interval: the index for an N-element array comes from [0,N). I find that this > makes things simpler than with 1-based arrays, because half-open intervals can > be abutted without having to adjust by one. (I.e., [0,N) U [N,P) = [0,P).) How is that any different than for 1-based arrays? [1,N) U [N,P) = [1,P), non? In both of the rebuttals to the pro-1-based-array postings, I didn't see anything that said where 1-based arrays fall down. The only problem I see with them is efficiency: you have to subtract 1 (or 2 or 4 or sizeof(element)) when you index into the array. Or do you? There's nothing to say that the compiler can't actually allocate an array one element bigger than you ask for, and just waste the first element, is there? If you've got the memory to burn. C, of course would be in trouble with this approach, since arrays in C are so often traversed by pointer manipulation. But in languages that don't allow this (and, I hope, their compilers produce similar code anyway), there shouldn't be a problem. As someone said, one-based arrays often seem more natural. I want to store 10 of these things. So I allocate a ten element array. Now, quick, which is the last element? Number NINE? Huh? -- -Peter Schachte pds@quintus.uucp ...!sun!quintus!pds