Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.fortran,comp.lang.c++ Subject: Re: =?utf-8?Q?=E2=80=9CWhy?= do arrays start at 0?" Date: Sat, 27 Aug 2022 22:07:27 -0700 Organization: None to speak of Lines: 39 Message-ID: <878rn9j6r4.fsf@nosuchdomain.example.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: reader01.eternal-september.org; posting-host="c4eb9558376fc13f96f34a0aea0355d2"; logging-data="592493"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18cr1jAW4+hJCYsYMdHR4PW" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:oaX9R0usvECw7ROorkgRVgicRyw= sha1:L8g+Cb4rtz54z6STOz2ga61G41I= Xref: csiph.com comp.lang.fortran:73536 comp.lang.c++:86128 Lynn McGuire writes: > On 8/27/2022 5:24 PM, Thomas Koenig wrote: >> Lynn McGuire schrieb: >>> On 8/27/2022 2:26 PM, Fred. Zwarts wrote: >>>> Op 26.aug..2022 om 22:49 schreef Lynn McGuire: >>>>> “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 assumed that it was done because in C x[i] is equivalent to *(x+i). >>> >>> Yup. So Fortran x(i) is equivalent to *(x+i-1). >> To be more precise, x(i) is equivalent to *(x+i-lbound(x,1)) >> >>> Or, the x is >>> subtracted from first: x--; *(x+i);. >> That's an f2c idiom, which is not valid C, AFAIK, because >> the pointer would point before the actual array. > > While the second pointer is a bad pointer, it is not a illegal > pointer. Variables can point to anywhere, they are not illegal until > referenced. Otherwise, code would be crashing all over the place. If > I malloc'd some space, then free'd the space, and did not NULL the > pointer, that dangling pointer would crash if ever referenced > (hopefully) but not if it just hangs around. Computing a pointer before the beginning of an array causes undefined behavior in C and C++, even if you never dereference it. [...] -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips void Void(void) { Void(); } /* The recursive call of the void */