Women in Technology

Hear us Roar



Article:
  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?

Full Threads Newest First

Showing messages 1 through 26 of 26.

  • 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. :-)

    • C, C++, Java, maybe others - zeroth element arrays
      2003-05-14 09:33:22  anonymous2 [View]

      My point was that to think about the thing in a way that is consistent with the tangible world I am forced to treat the concept of zero as something other than zero.

      If you're counting your children, which one is the zeroth kid?

      Bending the humans to the machine, rather than bending the machine to the humans. Bad approach, IMHO.
      • C, C++, Java, maybe others - zeroth element arrays
        2003-05-15 02:07:51  anonymous2 [View]

        Perhaps you should move to COBOL :-), where the first element of an array is addressed as...... 1.
        • C, C++, Java, maybe others - zeroth element arrays
          2003-09-18 13:18:15  anonymous2 [View]

          This is a bit late, but here are my $0.02.

          I like arrays in pascal with bounds from -n to +n: var a: array [-10..10] of integer; It is very natural, simple and with run-time bounds checking also very safe. With arrays like pascal's much of current buffer overrun exploits would (probably) not exist!
          • C, C++, Java, maybe others - zeroth element arrays
            2005-02-01 22:58:52  Nurchi [View]

            As people mentioned before, Pascal had the best implementation for the indeces of the arrays, when you can specify the lower and upper limit of the array.
            But as we all know (well, most of us) the 0-based arrays go many years back, when C was created (actually, way before that).
            As we know, any array in C/C++ (C style array, not <Vector>) is just a pointer to the first element of the array, so declaration:
            int[] a;
            is equivalent to:
            int* b;

            The number in square brackets indicates offset from the first element, so
            "a[0]" means the same as "*b" and "a[5]" means the same as "(*(b+5))"
            It was made this way, some people think it is stupid, but talking about all the stupid things that happen in our world, let's blame Hitler for..., but would anything change?
            Just understand the concept and be consistent with it. I think it is kind of cool to use
            for (int i=0; i {
            //Do something here
            }

            The idea is that you go from 0 to 'i' strictly less than whatever, and as long as you are consistent with it, ...
            I think, the bounds of the arrays are fixed and there is nothing we can do, and I am used to it, and I personally (some of you might not agree, but I don't care) think, it should be this way now. It is like this, and it shouldn't be changed.

            AFAIK (As Far As I Know) even VB.NET has 0-based arrays now, starting from Visual Studio .NET 2002 (correct me if I am wrong).

            Anyway, I think, discussions are OK, but when people argue about what is better, and what is worse, it is just stupid.
            I (now) prefer 0-based arrays, and it would be hard for me (at least for the first time) to switch back to 1-based...
            Everyone should be good at something. If you're good at 1-based, there you go, use whatever programming language you want to use, or just create the array 1 element larger than you actually need it. That way you waste some memory on element with index 0, but you for sure can access elements from 1 up to (and including) MyArray.Length

            OR MAKE YOUR OWN DAMN COMPILER!!!!!!!!!!!!!!!

            Good luck.

            P.S.
            C makes it easy to shoot yourself in the foot;
            C++ makes it harder, but when you do,
            it blows away your whole leg."
            --Bjarne Stroustrup
  • 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 09:29:51  anonymous2 [View]

      I will assume your response was *not* tongue-in-cheek. (I posted the message you replied to.)

      Are you suggesting that *not* using zero means we may run out of digits? Ridiculous. Zero is not an element number - it is an offset.

      This is an example of bending the humans to the machine, rather than bending the machine to the humans. IMHO, a fundamental mistake in the approach.
      • C, C++, Java, maybe others - zeroth element arrays
        2003-05-14 09:55:38  anonymous2 [View]

        The "Why waste a perfectly good digit?" bit was tongue-in-cheek.

        But I've been programming so long I really can't imagine not using zero. It's second nature to me now.
        • C, C++, Java, maybe others - zeroth element arrays
          2003-05-14 10:54:13  anonymous2 [View]

          why arrays indexed from zero? cos everybody is used to them. maybe if we all started indexing arrays from 1 back in the day it'd seem sensible, but it has become convention. i'd find it weird if i moved to a new language and found the arrays indexed from 1, it'd spongle all my for-loop logic & annoy me every time i forgot it.
          • There's a zeroth element?
            2003-05-14 13:08:17  anonymous2 [View]

            Maybe I'm backwards, but I rarely use the zeroth element. I do know that C and its pointer arithmetic is why there is a zeroth element. But unless the problem absolutely requires it, I just ignore it like a bit of grissle on my steak.
          • C, C++, Java, maybe others - zeroth element arrays
            2003-05-14 14:39:09  anonymous2 [View]

            Naah, you don't really want me to reply with a list of languages that do suport arrays starting 'at one' do you:)
            Don't be worried if you ever encounter one though, those languages do not support for-loop logic either.
            You'll indeed need an all new logic:
            the first is in position one,
            the second is in position two,
            ...
            the Nth is in position N.

            Hey, I really do like C, but not really for business processing.
            • C, C++, Java, maybe others - zeroth element arrays
              2003-07-06 00:49:55  anonymous2 [View]

              FORTRAN and MATLAB both index from 1.

              I migrated from MATLAB to Numerical Python, just to get back to the sweet, nourishing 0 indexing.
  • 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-24 06:26:13  anonymous2 [View]

      With one dimensional array there is simple fix. When you have elem_t a[N], indexed from 0 to N-1, then elem_t *b = (a - 1) is indexed from 1 to N.
  • 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-15 00:33:37  anonymous2 [View]

      This is like saying a hole in the ground is better than a flushing toilet, it is giving you what you want and is less difficult to implement. And what if you are on rocky ground?

      I do not need to care about implementing multidimensional arrays into memory (that's the compilers task, right?), I just want to express my thoughts in the language easily. No cupboard in my house has a zeroth shelf.
      • C, C++, Java, maybe others - zeroth element arrays
        2003-05-15 03:52:41  anonymous2 [View]

        No, the cupboards in your house may not have a zeroth shelf, but they DO have bottom shelfs, which are really just the analog of the array index 0, in terms of sequence. In fact, I would propose that there really isn't a zeroth element in an array, aswell. (Much to the chagrin of what the "zeroth" jargon file entry author might have to say, I might add.)

        Zero is the offset from the beginning of the array that you use to access the First element. Perhaps there is an inevitable blur in the distinction between the indexer and the indexee when dealing with arrays.

        When conceptualising the digital representation of an array for your cupboard analog, you must note that it is neither wrong, nor right to assert that the first element in the array starts at 0; it is rather tradition, but useful nonetheless. Consider the following:

        In Python, for instance, you can access the last element in the array with the value -1. If the first element were 1, then accessing the last element via 0 would seem rather unnatural, both mathematically and practically, as access is being performed with offsets in mind.

        At the end of the day, it all boils down to the age-old philosophical quandary of the "half-filled cup" perspective. Either you approach the issue from the viewpoint that you are accessing the first element, with the quantitative equivalence of 1, or you are accessing the first element at the offset from the bottom/start of the array, or the sequential equivalence of 0.

        Trying to convince anyone that they are wrong in this matter is simply a fool's toil.
    • C, C++, Java, maybe others - zeroth element arrays
      2003-05-15 11:55:38  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."

      First of all, the distance is not the element number, it is the multiple of the element number times the size of an individual element in bytes.

      Regardless of that, here is where I disagree with you completely. The array index is for the PROGRAMMER. The offset is for the MACHINE. Becoming accustomed to thinking this way has nothing to do with the original point - which was that this is bending the humans to the machine rather than the other way around.
  • 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-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-10-24 13:24:10  anonymous2 [View]

      "Think of the indexes as *between* the elements..."

      What an absurd way to think of an array index. An array isn't a continuum, it's a numbered sequence of discrete items, so the rest of your "(0,1] [0,1]..." argument is irrelevant.

      How do you number three items in every other domain in life? 1, 2, and 3. The first element of an array of items should be array[1], the the Nth would be array[N]. An array whose highest legal subscript was N would have N elements.

      Easy to count, easy to refer to, the least suceptible of all systems to off-by-one bugs.

      this confusion comes from the fact that array subscripts in close-to-the-metal languages like C are just syntactic sugar for memory offsets. Naturally, the first element of an array starts where the array itself starts, so its offset is zero. my_array(N) is just my_array + N*sizeof(element). It's a pointer to a memory address.

      Unfortunately, from the very start, the most used languages have been low level, like C, so programmers have just gotten it in their heads that arrays *ought* to be zero-based. Perhaps they should be in such languages, but they carry that legacy mindset with them even when they move on to high-level languages where memory offsets are irrelevant -- to where you are simply labeling a sequence of items. If language designers now try to label array elements 1, 2, and 3 as 1, 2, and 3 (instead of 0, 1, and 2), though, the experienced (read: "those with the most legacy baggage between their ears") scream about it, coming up with preposterous, strained arguments such as the one above, appealing to "mathematics" to explain to gullible people why the ordinary counting everyone uses for everything else in life is somehow not acceptible for counting elements in an array.

      Nonsense.
  • 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-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-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-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
    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,
    Andrew