Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #86106 > unrolled thread
| Started by | Lynn McGuire <lynnmcguire5@gmail.com> |
|---|---|
| First post | 2022-08-26 15:49 -0500 |
| Last post | 2022-08-29 11:15 +0000 |
| Articles | 5 on this page of 25 — 14 participants |
Back to article view | Back to comp.lang.c++
“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?" 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: 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?" Louis Krupp <lkrupp@invalid.pssw.com.invalid> - 2022-08-26 16:08 -0600
Re: “Why do arrays start at 0?" Gawr Gura <gawrgura@mail.hololive.com> - 2022-08-26 15:18 -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?" 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?" 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?" 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?" 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?" Juha Nieminen <nospam@thanks.invalid> - 2022-08-29 11:15 +0000
Page 2 of 2 — ← Prev page 1 [2]
| From | Paavo Helde <eesnimi@osa.pri.ee> |
|---|---|
| Date | 2022-08-28 19:43 +0300 |
| Message-ID | <teg5v0$lrls$1@dont-email.me> |
| In reply to | #86127 |
28.08.2022 06:40 Lynn McGuire kirjutas: > On 8/27/2022 5:24 PM, Thomas Koenig wrote: >> Lynn McGuire <lynnmcguire5@gmail.com> 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. >>>>> >>>>> Lynn >>>>> >>>> >>>> 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. The C++ standard (n4861) calls this "an invalid pointer value". For pointers which continue to point to freed objects, it says "A pointer value becomes invalid when the storage it denotes reaches the end of its storage duration". About invalid pointers it says: "Indirection through an invalid pointer value and passing an invalid pointer value to a deallocation function have undefined behavior. Any other use of an invalid pointer value has implementation-defined behavior." There is also a footnote: "Some implementations might define that copying an invalid pointer value causes a system-generated runtime fault." So, while technically one might get away with the "x--" trick most of the time with the linear memory addressing used by mainstream implementations nowadays, still various diagnostic tools would mark these as invalid pointers, causing an avalanche of errors whenever you want to solve your actual memory access problems. I guess it might also subvert automatic garbage collection which is sometimes used with C++. There is a special case of "safely-derived" pointer values which I suspect is made exactly for making GC possible, and changing the pointer value to x-1 would apparently ruin this. And with segmented memory, like with 16-bit x86, it might cause all kind of surprises.
[toc] | [prev] | [next] | [standalone]
| From | "Fred. Zwarts" <F.Zwarts@KVI.nl> |
|---|---|
| Date | 2022-08-28 10:01 +0200 |
| Message-ID | <tef7ci$12uj$1@gioia.aioe.org> |
| In reply to | #86124 |
Op 27.aug..2022 om 22:01 schreef Lynn McGuire: > 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. >>> >>> Lynn >>> >> >> 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). Or, the x is > subtracted from first: x--; *(x+i);. > > Lynn > I don't understand the idea of x--. Why modifying x? What happens if x is indexed later again?
[toc] | [prev] | [next] | [standalone]
| From | Thomas Koenig <tkoenig@netcologne.de> |
|---|---|
| Date | 2022-08-28 08:14 +0000 |
| Message-ID | <tef85g$28i$1@newsreader4.netcologne.de> |
| In reply to | #86131 |
Fred. Zwarts <F.Zwarts@KVI.nl> schrieb: > Op 27.aug..2022 om 22:01 schreef Lynn McGuire: >> 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. >>>> >>>> Lynn >>>> >>> >>> 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). Or, the x is >> subtracted from first: x--; *(x+i);. >> >> Lynn >> > > I don't understand the idea of x--. Why modifying x? What happens if x > is indexed later again? The idea is to use this modified pointer for one-based array accesses, so that it would be possible to translate Fortran's A(1) into a[1] on the C side, or A(N) into a[n]. The correct way to do this according to the C standard would be to translate A(N) into a[n-1] on the C side. There are several reasons why this might not have been done: Readability of the generated code (although f2c code is already hard to read), because it made the code slower with compilers of the day, and probably because it "just worked" with the compilers.
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2022-08-28 11:07 +0200 |
| Message-ID | <tefb7v$javh$1@dont-email.me> |
| In reply to | #86106 |
Am 26.08.2022 um 22:49 schrieb 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. > > Lynn On the CPU-level you heave the least number of calculations to determine an address of an indexed entity if the index starts at zero.
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2022-08-29 11:15 +0000 |
| Subject | Re: ???Why do arrays start at 0?" |
| Message-ID | <tei759$19qc$1@gioia.aioe.org> |
| In reply to | #86106 |
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).
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.c++
csiph-web