What I Hate About Your Programming Language
Subject:   C, C++, Java, maybe others - zeroth element arrays
Date:   2003-05-14 06:51:27
From:   anonymous2
I hate zero-based array indexing. There is an IMMEDIATE disconnect when you declare an array to have 10 elements, then can only address up to element 9. Couldn't the COMPILER handle this transition so I can think about the array elements naturally?!?

What the heck is the ZEROth day of the week?

Main Topics Oldest First

Showing messages 1 through 11 of 11.

  • C, C++, Java, maybe others - zeroth element arrays
    2004-02-16 10:42:43  andrewjmarshall [View]

    Here's the final answer and I DON'T want to hear any arguments, because they're WRONG.

    For low/machine-level languages, arrays should be zero-based. There's too many technical reasons for this to argue against.

    For ALL other languages, array bounds should be CONFIGURABLE. Sometimes you want zero-based (freaks), ALMOST ALWAYS you want one-based, and sometimes you want a custom base. Let's remember one of the prime tenets of OO - Encapsulation or keep your users insulated from your implementatation. Why screw your programmers because it's "easier" for the compiler? F*** the compiler! Why is it my job to make the compiler's life easier?

    I spent 2 hours arguing this point with my brother-in-law (a UVa Computer Engineering grad), and he lost. So there!

    Hugs and kisses,
  • C, C++, Java, maybe others - zeroth element arrays
    2003-12-03 12:51:43  anonymous2 [View]

    I believe talking about the architecture of the machine or the storage mechanics of the language is irrelavant.

    An origin zero and one can BOTH be useful in DIFFERENT situations. What origin produces the cleanest algorithm for a person to follow?
    (modulo arithmetic, use zero base)

    A language that can switch between an index origin of zero or one globally inclusive of all functions, operators, classes, objects that might use indexing upon command should be implemented.

    If the language could address multidimensional arrays, again the emphasis could be on clean algorithms.

    Wait, that was done back in the early 1960's the language is APL.
  • C, C++, Java, maybe others - zeroth element arrays
    2003-10-31 20:22:38  bioinfotools [View]

    Both 0-based and 1-based indexing can be useful in different stituations. Personally, I think this is one of the things Pascal got right. In Pascal, you explicitly set both the upper and lower bounds of arrays, which are bounds checked. You make indices any valid integer range, including using 0 and negative values. This way you get both 0-based indices (to masquerade offsets), indices starting in 1, or indices ranging over precisely whatever you like (e.g. -100..100). From vague memory you could even index arrays with characters or elements of sets.
  • C, C++, Java, maybe others - zeroth element arrays
    2003-07-20 23:27:21  anonymous2 [View]

    Well, it's like some countries count the "first floor" as the "ground floor", whereas some other countries count "first floor" and "ground floor" differently...

    Note that those countries that count the "first floor" as the "ground floor" typically count "lower ground" floors as "basements" (e.g. "B1" or "basement 1" level)... Hence skipping the "zeroth" floor entirely... Whereas "ground floor" countries count it as "zero floor", and count the "B1" level equivalent as "lower ground" level(s).

    "0" index arrays has its uses, but from a memory management perspective in terms of C (and C++), it makes sense... For every other language, it MAY be different (JScript allows non-zero aligned arrays).

    Just like different languages has its own uses, the indexing schemes has its own uses as well...

    No use quarrelling over it, UNLESS you're designing a new language, then you can suitably implement the "proper" indexing scheme that SUITS the PURPOSE (not the language/architecture/etc).
  • C, C++, Java, maybe others - zeroth element arrays
    2003-06-30 05:41:05  anonymous2 [View]

    The main reason I prefer 0-indexing over 1-indexing
    is that modulo arithmetic works cleanly.
  • C, C++, Java, maybe others - zeroth element arrays
    2003-05-15 17:24:19  anonymous2 [View]

    Short version:
    1. Ask not whether to start at zero or one. Ask whether to place zero at the start of the array or the end.

    2. Ask not whether to use zero or one based indexing. Ask whether to use [0, 1) or (0, 1] based indexing.

    Long version:
    Think of the indexes as *between* the elements. Like inches on a ruler. The first inch is between the zero mark and the one mark. So we call that inch [0, 1). If we want the second and third inches together we want [1, 3). That is starting at the 1 mark and ending at the 3 mark. (Note that 3 minus 1 is the number of inches as nice side effect.) You also see this effect in birthdays. Your first year of live is between your zero'th birthday and your first birthday and so on.

    Now programmer hate to have to write [n, n+1) (or [n - 1, n)) all the time so we need a shorthand. We can take either the first number (zero based) or the second number (one based). It usually wont matter unless your array can use negative numbers. The most common (but not only) case of this is a circular array where negative numbers index from the end of the array. Zero will have to be placed either at the end or the begining of the array (we're *not* going to number them like we do our years). So the real question is whether zero goes at the begining or the end of the array. (Keep in mind where talking about shorthand notation. They are really elements [0, 1) and [-1, 0).)

    Note that 0 is an element of [0, 1) but 1 isn't so we should prefer calling it 0 instead of 1. This may be a bias because we could also use [0, 1], (0, 1), or (0, 1]. Let's examine these. I shouldn't have to explain why [0, 1] and (0, 1) are completely out of the question (ask any Math professor). So that brings it down to [0, 1) vs. (0, 1]. In a certain sence which we choose is arbitrary, but Mathematicians have generally settled on [0, 1). Maybe it's because they are more intrested in the set of non-negative numbers, [0, INF), than the set of non-positive numbers, (-INF, 0]. I don't know why but it *is* the convention to prefer [x, y) (read "x upto but not including y") over (x, y] (I have no idea how to read that).
  • C, C++, Java, maybe others - zeroth element arrays
    2003-05-15 01:41:27  anonymous2 [View]

    I know that others breifly mentioned why the zero is used, but clearly a better explanation is needed to enlighten those that think that it should start at 1. An array is an address. It is not some magic black box that has numbers. The address points to the first element of the block of memory that is the array. So lets say we have 'int array[10];'. 'array' is the same address as 'array[0]'. So the point is that you are asking how much should be add to the address to get to the first element. Add nothing (zero). Now lets say that we had compiler that did the index beginning at 1. If we explicitely said 'array[1]' this could be easily coded in assembly to access with a 0 offset, but what if we had 'array[i]'. The assembly must access the value of i and then subtract one. This adds an extra instruction that is otherwise not necessary. Clearly you know nothing about pointer or else it would be obvious about the arbitrary choice between using one or another. If I say 'int * ptr; ptr = array;', I can say '*(ptr + n)' to mean 'array[n]'; Is it clear now why this is the way it is. You clearly don't program in C much and don't know about the underworkings of the architecture.
  • C, C++, Java, maybe others - zeroth element arrays
    2003-05-14 17:50:13  anonymous2 [View]

    You and all the other replies seem to forget
    the most important thing: an array index measures
    the distance of the element from the beginning
    of the array. If you think otherwise, try to
    write a piece of code that maps a 6 dimension
    array into a the memory (which is a one dimension
    array) in a language that starts indexing with 1.

    The mess that you will see is the reason why it
    is better to start with 0. Also, if you like
    starting with 1, cut off the 0th inch from all
    of your rulers and measuring tapes and try to build a shed (or do any job where you have to
    measure things). I would like to see the result
    or how far you get.
  • C, C++, Java, maybe others - zeroth element arrays
    2003-05-14 13:15:40  anonymous2 [View]

    Because it's not always just a compile time fix. If a variable is used in the index, the 'minus one' must be done at runtime. In math intensive stuff, this can be devastating. The best thing I think is a 1-based default, and an option to revert to 0-base on specific arrays.
  • C, C++, Java, maybe others - zeroth element arrays
    2003-05-14 08:46:50  anonymous2 [View]

    I can't imagine *not* using zero-based arrays. Why waste a perfectly good digit?
  • C, C++, Java, maybe others - zeroth element arrays
    2003-05-14 07:32:21  anonymous2 [View]

    Zero-based indexing isn't so bad. If you MUST, you can alwas use enumerated indexing, or fake enumerated indexing with some constant definitions.

    And, for things like 'cron,' the zeroth day of the week is Sunday. :-)