Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Any sufficiently advanced technology is indistinguishable from a rigged demo.


devel / comp.lang.fortran / Re: ???Why do arrays start at 0?"

SubjectAuthor
* ???Why do arrays start at 0?"Juha Nieminen
`- ???Why do arrays start at 0?"gah4

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

<tei759$19qc$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=9883&group=comp.lang.fortran#9883

  copy link   Newsgroups: comp.lang.fortran comp.lang.c++
Path: i2pn2.org!i2pn.org!aioe.org!NK0c7qMEn6mmBWqphs27pg.user.46.165.242.75.POSTED!not-for-mail
From: nos...@thanks.invalid (Juha Nieminen)
Newsgroups: comp.lang.fortran,comp.lang.c++
Subject: Re: ???Why do arrays start at 0?"
Date: Mon, 29 Aug 2022 11:15:56 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <tei759$19qc$1@gioia.aioe.org>
References: <tebblf$2gpm$1@dont-email.me>
Injection-Info: gioia.aioe.org; logging-data="42828"; posting-host="NK0c7qMEn6mmBWqphs27pg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: tin/2.4.3-20181224 ("Glen Mhor") (UNIX) (Linux/5.10.103-grsec-kapsi (x86_64))
X-Notice: Filtered by postfilter v. 0.9.2
 by: Juha Nieminen - Mon, 29 Aug 2022 11:15 UTC

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

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

<317d57ec-0fbe-476a-98b9-a33f4c369b1bn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=9896&group=comp.lang.fortran#9896

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a05:620a:178c:b0:6bb:b3a8:88f9 with SMTP id ay12-20020a05620a178c00b006bbb3a888f9mr10901898qkb.759.1661839140543;
Mon, 29 Aug 2022 22:59:00 -0700 (PDT)
X-Received: by 2002:a25:bdd4:0:b0:696:4dbd:dcc9 with SMTP id
g20-20020a25bdd4000000b006964dbddcc9mr10937196ybk.392.1661839140218; Mon, 29
Aug 2022 22:59:00 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Mon, 29 Aug 2022 22:59:00 -0700 (PDT)
In-Reply-To: <tei759$19qc$1@gioia.aioe.org>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:602:9700:4689:9436:1cc2:ea0:7fd3;
posting-account=gLDX1AkAAAA26M5HM-O3sVMAXdxK9FPA
NNTP-Posting-Host: 2601:602:9700:4689:9436:1cc2:ea0:7fd3
References: <tebblf$2gpm$1@dont-email.me> <tei759$19qc$1@gioia.aioe.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <317d57ec-0fbe-476a-98b9-a33f4c369b1bn@googlegroups.com>
Subject: Re: ???Why do arrays start at 0?"
From: gah...@u.washington.edu (gah4)
Injection-Date: Tue, 30 Aug 2022 05:59:00 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 5093
 by: gah4 - Tue, 30 Aug 2022 05:59 UTC

On Monday, August 29, 2022 at 4:16:00 AM UTC-7, Juha Nieminen wrote:

(snip)

> 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.)
BCPL, a predecessor of C, was written on a word addressable
machine. Pointer arithmetic was integer arithmetic. It was with
C that variables could be larger than the addressable unit,
and that pointers (addresses) incremented and decremented
in appropriate sized units.

In any case, early C (and B and BCPL) programmers were likely
also assembly programmers, and would have been used
to 0 origin indexing.

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

For many machines, you have to multiply by the element size,
but yes some do it for you. C pointer arithmetic is defined in terms
of the size of the pointed-to object, and array indexing is defined
in terms of pointer arithmetic.
> 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).

In the case of Fortran, starting with static arrays, the compiler can subtract
before generating the address constant, such that the subtract is done
at compile time. No run-time cost.

For allocatable arrays, it can adjust the offset once, and use that.

For automatic variables on the stack, the stack pointer offset is
computed once.

The way C pointers are defined, that would not be quite as
easy to do. Or, C pointers are defined the way they are to make it easier.

Fortran was originally Formula Translation, and mathematicians did,
and still do, use 1 based indexing for matrices and vectors.

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

Note that it gets more interesting for Fortran. When you pass an array
as an argument to a subroutine, it loses the original origin.
(Well, most of the time. Remembering when it does and doesn't is
probably harder than figuring out 0 vs.1 origin.)

One that C programmers like to do, and I suspect was faster on
many early machines, is index through arrays with a pointer to an array,
indexing it as it goes. That means no subscript calculation in the loop.
Many early C processors didn't have multiply, or had slow multiply.

However, one of the favorite optimizations for Fortran compilers
is strength reduction, converting multiply inside a loop into addition
as the loop increments. The result is pretty much the same.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor