Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.fortran > #73564

Re: ???Why do arrays start at 0?"

From Juha Nieminen <nospam@thanks.invalid>
Newsgroups comp.lang.fortran, comp.lang.c++
Subject Re: ???Why do arrays start at 0?"
Date 2022-08-29 11:15 +0000
Organization Aioe.org NNTP Server
Message-ID <tei759$19qc$1@gioia.aioe.org> (permalink)
References <tebblf$2gpm$1@dont-email.me>

Cross-posted to 2 groups.

Show all headers | View raw


In comp.lang.c++ Lynn McGuire <lynnmcguire5@gmail.com> wrote:
> ???Why do arrays start at 0?"
>    https://buttondown.email/hillelwayne/archive/why-do-arrays-start-at-0/
> 
> "It's not the reason you think. No, it's not that reason either.???
> 
> My Fortran starts at one.  My C++ starts at zero.  This has made my life 
> hell.

I don't know if it's the *original* reason, but I would assume that at least
in C one of the main reasons is the principle of maximum efficiency.

In many processor architectures the concept of "array" exists, at least
when it comes to values of the register sizes (ie. usually 1-byte,
2-byte, 4-byte and 8-byte elements, the last one at least on 64-bit
architectures). Prominently the concept of an indexable array exists
in the x86 architecture. (I don't remember now if it also exists in
the ARM architecture, but I would guess so.)

Generally when a processor architecture supports the concept of an "array",
it does so by having instructions that take (at least) two registers as
the input or the output parameter: A base address, and an offset. The
memory location of the element is calculated by adding those two. (The
number of bytes that an offset of 1 jumps depends on the instruction,
and thus multi-byte elements are supported.)

Thus zero-indexing is extraordinarily natural in processor architectures:
The "index" is actually an offset. It's a value you add to the base
address in order to get to the location you want. Thus, the first element
is at index/offset 0.

Since that's the case, the most optimal way to handle low-level arrays is
to have 0-based indexing in the programming language as well. That way
you don't need to be subtracting 1 from the index every time an array is
accessed (or you don't need an extraneous unused element at the beginning
of the array, consuming memory for no reason).

Also, since C supports pointer arithmetic, many operations become simpler.
Such as getting the index of an element when what you have is a pointer to
it (and the pointer to the start of the array).

Back to comp.lang.fortran | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

“Why do arrays start at 0?" Lynn McGuire <lynnmcguire5@gmail.com> - 2022-08-26 15:49 -0500
  Re: “Why do arrays start at 0?" Gary Scott <garylscott@sbcglobal.net> - 2022-08-26 16:56 -0500
    Re: “Why do arrays start at 0?" Mr Flibble <flibble@reddwarf.jmc.corp> - 2022-08-27 00:46 +0100
      Re: “Why do arrays start at 0?" d thiebaud <thiebauddick2@aol.com> - 2022-08-26 22:29 -0400
        Re: “Why do arrays start at 0?" gah4 <gah4@u.washington.edu> - 2022-08-26 19:34 -0700
        Re: “Why do arrays start at 0?" Robin Vowels <robin.vowels@gmail.com> - 2022-08-26 20:34 -0700
        Re: “Why do arrays start at 0?" Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-26 22:05 -0700
          Re: “Why do arrays start at 0?" Mr Flibble <flibble@reddwarf.jmc.corp> - 2022-08-27 12:06 +0100
    Re: “Why do arrays start at 0?" gah4 <gah4@u.washington.edu> - 2022-08-26 18:24 -0700
    Re: Re: “Why do arrays start at 0?" scott@slp53.sl.home (Scott Lurndal) - 2022-08-27 14:28 +0000
    Re: �Why do arrays start at 0?" Charlie Roberts <croberts@gmail.com> - 2022-08-28 10:25 -0400
  Re: “Why do arrays start at 0?" Louis Krupp <lkrupp@invalid.pssw.com.invalid> - 2022-08-26 16:08 -0600
    Re: �Why do arrays start at 0?" Charlie Roberts <croberts@gmail.com> - 2022-08-28 10:28 -0400
      Re: “Why do arrays start at 0?" Thomas Koenig <tkoenig@netcologne.de> - 2022-08-28 16:30 +0000
      Re: “Why do arrays start at 0?" Ron Shepard <nospam@nowhere.org> - 2022-08-29 02:42 -0500
        Re: “Why do arrays start at 0?" Thomas Koenig <tkoenig@netcologne.de> - 2022-08-30 17:18 +0000
          Re: “Why do arrays start at 0?" gah4 <gah4@u.washington.edu> - 2022-08-30 12:15 -0700
  Re: “Why do arrays start at 0?" Mr Flibble <flibble@reddwarf.jmc.corp> - 2022-08-27 00:44 +0100
  Re: “Why do arrays start at 0?" Robin Vowels <robin.vowels@gmail.com> - 2022-08-26 20:31 -0700
    Re: “Why do arrays start at 0?" "fiz...@gmail.com" <fiziqs@gmail.com> - 2022-08-27 01:48 -0700
      Re: “Why do arrays start at 0?" gah4 <gah4@u.washington.edu> - 2022-08-27 02:17 -0700
        Re: “Why do arrays start at 0?" Thomas Koenig <tkoenig@netcologne.de> - 2022-08-27 09:30 +0000
          Re: “Why do arrays start at 0?" gah4 <gah4@u.washington.edu> - 2022-08-27 10:01 -0700
            Re: “Why do arrays start at 0?" Thomas Koenig <tkoenig@netcologne.de> - 2022-08-27 22:13 +0000
              Re: “Why do arrays start at 0?" gah4 <gah4@u.washington.edu> - 2022-08-27 22:26 -0700
                Re: “Why do arrays start at 0?" Thomas Koenig <tkoenig@netcologne.de> - 2022-08-28 06:59 +0000
                Re: “Why do arrays start at 0?" Lynn McGuire <lynnmcguire5@gmail.com> - 2022-08-28 13:42 -0500
                Re: “Why do arrays start at 0?" Thomas Koenig <tkoenig@netcologne.de> - 2022-08-28 20:17 +0000
                Re: “Why do arrays start at 0?" gah4 <gah4@u.washington.edu> - 2022-08-28 14:08 -0700
                Re: “Why do arrays start at 0?" gah4 <gah4@u.washington.edu> - 2022-08-28 14:05 -0700
                Re: “Why do arrays start at 0?" Robin Vowels <robin.vowels@gmail.com> - 2022-08-28 21:54 -0700
                Re: “Why do arrays start at 0?" Robin Vowels <robin.vowels@gmail.com> - 2022-08-28 21:53 -0700
              Re: “Why do arrays start at 0?" Ron Shepard <nospam@nowhere.org> - 2022-08-29 02:07 -0500
                Re: “Why do arrays start at 0?" Robin Vowels <robin.vowels@gmail.com> - 2022-08-29 01:30 -0700
                Re: “Why do arrays start at 0?" Ron Shepard <nospam@nowhere.org> - 2022-08-29 10:13 -0500
                Re: “Why do arrays start at 0?" Robin Vowels <robin.vowels@gmail.com> - 2022-08-29 12:30 -0700
                Re: “Why do arrays start at 0?" gah4 <gah4@u.washington.edu> - 2022-08-29 03:48 -0700
      Re: “Why do arrays start at 0?" Robin Vowels <robin.vowels@gmail.com> - 2022-08-27 05:16 -0700
  Re: “Why do arrays start at 0?" Thomas Koenig <tkoenig@netcologne.de> - 2022-08-27 05:47 +0000
    Re: “Why do arrays start at 0?" d thiebaud <thiebauddick2@aol.com> - 2022-08-27 15:19 -0400
      Re: “Why do arrays start at 0?" FortranFan <parekhvs@gmail.com> - 2022-08-27 12:45 -0700
      Re: “Why do arrays start at 0?" Thomas Koenig <tkoenig@netcologne.de> - 2022-08-27 22:22 +0000
  Re: “Why do arrays start at 0?" "Fred. Zwarts" <F.Zwarts@KVI.nl> - 2022-08-27 21:26 +0200
    Re: “Why do arrays start at 0?" Lynn McGuire <lynnmcguire5@gmail.com> - 2022-08-27 15:01 -0500
      Re: “Why do arrays start at 0?" Thomas Koenig <tkoenig@netcologne.de> - 2022-08-27 22:24 +0000
        Re: “Why do arrays start at 0?" John <urbanjost@comcast.net> - 2022-08-27 19:34 -0700
          Re: “Why do arrays start at 0?" Robin Vowels <robin.vowels@gmail.com> - 2022-08-28 01:50 -0700
        Re: “Why do arrays start at 0?" Lynn McGuire <lynnmcguire5@gmail.com> - 2022-08-27 22:40 -0500
          Re: “Why do arrays start at 0?" Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-27 22:07 -0700
            Re: “Why do arrays start at 0?" gah4 <gah4@u.washington.edu> - 2022-08-27 23:04 -0700
          Re: “Why do arrays start at 0?" Thomas Koenig <tkoenig@netcologne.de> - 2022-08-28 07:50 +0000
            Re: “Why do arrays start at 0?" David Brown <david.brown@hesbynett.no> - 2022-08-28 11:52 +0200
          Re: “Why do arrays start at 0?" Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-28 19:43 +0300
      Re: “Why do arrays start at 0?" "Fred. Zwarts" <F.Zwarts@KVI.nl> - 2022-08-28 10:01 +0200
        Re: “Why do arrays start at 0?" Thomas Koenig <tkoenig@netcologne.de> - 2022-08-28 08:14 +0000
  Re: “Why do arrays start at 0?" Bonita Montero <Bonita.Montero@gmail.com> - 2022-08-28 11:07 +0200
    Re: “Why do arrays start at 0?" gah4 <gah4@u.washington.edu> - 2022-08-28 02:55 -0700
    Re: “Why do arrays start at 0?" Robin Vowels <robin.vowels@gmail.com> - 2022-08-28 05:47 -0700
  Re: ???Why do arrays start at 0?" Juha Nieminen <nospam@thanks.invalid> - 2022-08-29 11:15 +0000
    Re: ???Why do arrays start at 0?" gah4 <gah4@u.washington.edu> - 2022-08-29 22:59 -0700

csiph-web