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 | 20 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 1 of 2 [1] 2 Next page →
| From | Lynn McGuire <lynnmcguire5@gmail.com> |
|---|---|
| Date | 2022-08-26 15:49 -0500 |
| Subject | “Why do arrays start at 0?" |
| Message-ID | <tebblf$2gpm$1@dont-email.me> |
“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
[toc] | [next] | [standalone]
| From | Gary Scott <garylscott@sbcglobal.net> |
|---|---|
| Date | 2022-08-26 16:56 -0500 |
| Message-ID | <tebfiv$2rs6$1@dont-email.me> |
| In reply to | #86106 |
On 8/26/2022 3:49 PM, Lynn McGuire 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. > > Lynn > Fortran was there first...and got it right...you can change Fortran to start at other index values for many cases.
[toc] | [prev] | [next] | [standalone]
| From | Mr Flibble <flibble@reddwarf.jmc.corp> |
|---|---|
| Date | 2022-08-27 00:46 +0100 |
| Message-ID | <20220827004602.000075b1@reddwarf.jmc.corp> |
| In reply to | #86110 |
On Fri, 26 Aug 2022 16:56:47 -0500 Gary Scott <garylscott@sbcglobal.net> wrote: > On 8/26/2022 3:49 PM, Lynn McGuire 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. > > > > Lynn > > > Fortran was there first...and got it right...you can change Fortran > to start at other index values for many cases. Being first doesn't make you correct. Fortran is retarded. /Flibble
[toc] | [prev] | [next] | [standalone]
| From | d thiebaud <thiebauddick2@aol.com> |
|---|---|
| Date | 2022-08-26 22:29 -0400 |
| Message-ID | <tebvj6$1chn$1@gioia.aioe.org> |
| In reply to | #86114 |
On 8/26/22 19:46, Mr Flibble wrote: > On Fri, 26 Aug 2022 16:56:47 -0500 > Gary Scott <garylscott@sbcglobal.net> wrote: > >> On 8/26/2022 3:49 PM, Lynn McGuire 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. >>> >>> Lynn >>> >> Fortran was there first...and got it right...you can change Fortran >> to start at other index values for many cases. > > Being first doesn't make you correct. Fortran is retarded. > > /Flibble > A reasonable language would let you specify the lower index. Fortran and C are both retarded.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2022-08-26 22:05 -0700 |
| Message-ID | <87h71yjmya.fsf@nosuchdomain.example.com> |
| In reply to | #86115 |
d thiebaud <thiebauddick2@aol.com> writes:
> On 8/26/22 19:46, Mr Flibble wrote:
>> On Fri, 26 Aug 2022 16:56:47 -0500
>> Gary Scott <garylscott@sbcglobal.net> wrote:
>>
>>> On 8/26/2022 3:49 PM, Lynn McGuire 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.
>>>>
>>>> Lynn
>>>>
>>> Fortran was there first...and got it right...you can change Fortran
>>> to start at other index values for many cases.
>> Being first doesn't make you correct. Fortran is retarded.
>> /Flibble
>>
> A reasonable language would let you specify the lower index. Fortran
> and C are both retarded.
"Mr Flibble" is a troll, and I'm sure he's aware that "retarded" is an
offensive word. Are you?
--
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 */
[toc] | [prev] | [next] | [standalone]
| From | Mr Flibble <flibble@reddwarf.jmc.corp> |
|---|---|
| Date | 2022-08-27 12:06 +0100 |
| Message-ID | <20220827120610.00002dd0@reddwarf.jmc.corp> |
| In reply to | #86116 |
On Fri, 26 Aug 2022 22:05:17 -0700 Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: > d thiebaud <thiebauddick2@aol.com> writes: > > On 8/26/22 19:46, Mr Flibble wrote: > >> On Fri, 26 Aug 2022 16:56:47 -0500 > >> Gary Scott <garylscott@sbcglobal.net> wrote: > >> > >>> On 8/26/2022 3:49 PM, Lynn McGuire 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. > >>>> > >>>> Lynn > >>>> > >>> Fortran was there first...and got it right...you can change > >>> Fortran to start at other index values for many cases. > >> Being first doesn't make you correct. Fortran is retarded. > >> /Flibble > >> > > A reasonable language would let you specify the lower index. Fortran > > and C are both retarded. > > "Mr Flibble" is a troll, and I'm sure he's aware that "retarded" is an > offensive word. Are you? Retarded isn't an offensive word but I could replace it with fucktarded if that helps? /Flibble
[toc] | [prev] | [next] | [standalone]
| From | scott@slp53.sl.home (Scott Lurndal) |
|---|---|
| Date | 2022-08-27 14:28 +0000 |
| Message-ID | <BSpOK.163814$ze2.78928@fx36.iad> |
| In reply to | #86110 |
Gary Scott <garylscott@sbcglobal.net> writes: >On 8/26/2022 3:49 PM, Lynn McGuire 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. >> >> Lynn >> >Fortran was there first...and got it right...you can change Fortran to >start at other index values for many cases. There are around twenty languages that start array indicies with one. None of them are low-level hardware-oriented languages (Burroughs algol excepted) like C. In the hardware indicies start at zero by definition.
[toc] | [prev] | [next] | [standalone]
| From | Louis Krupp <lkrupp@invalid.pssw.com.invalid> |
|---|---|
| Date | 2022-08-26 16:08 -0600 |
| Message-ID | <pvbOK.866404$ntj.801728@fx15.iad> |
| In reply to | #86106 |
On 8/26/2022 2:49 PM, Lynn McGuire 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. > Here's a crude outline of a solution: If you want a C++ array index to start at 1, make the array one element bigger than necessary, and start indexing at 1, just like in Fortran. Element 0 will be unused. If you want a C++ array to map exactly to a Fortran array, but with an index starting at 1, wrap the C++ array in a class and overload the subscripting operator(s) to subtract 1 from the index. Neither of these approaches seems elegant, but one of them should be less hellish than the other. Louis
[toc] | [prev] | [next] | [standalone]
| From | Gawr Gura <gawrgura@mail.hololive.com> |
|---|---|
| Date | 2022-08-26 15:18 -0700 |
| Message-ID | <tebgs3$31ke$1@dont-email.me> |
| In reply to | #86106 |
On 8/26/22 13:49, Lynn McGuire 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. > > Lynn > I've never personally had trouble with array indices but row-major vs column-major order on matrices always gets me (especially going back and forth between C++ and GLSL). Luckily, C++ gives you plenty of tools to make your own arrays and matrices with the interface you prefer.
[toc] | [prev] | [next] | [standalone]
| From | Mr Flibble <flibble@reddwarf.jmc.corp> |
|---|---|
| Date | 2022-08-27 00:44 +0100 |
| Message-ID | <20220827004445.00006a85@reddwarf.jmc.corp> |
| In reply to | #86106 |
On Fri, 26 Aug 2022 15:49:51 -0500 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. Fortran is retarded. /Flibble
[toc] | [prev] | [next] | [standalone]
| From | Thomas Koenig <tkoenig@netcologne.de> |
|---|---|
| Date | 2022-08-27 05:47 +0000 |
| Message-ID | <tecb60$5sh$1@newsreader4.netcologne.de> |
| In reply to | #86106 |
Lynn McGuire <lynnmcguire5@gmail.com> schrieb: > “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. If you want to declare your Fortran arrays to start at zero, just declare them with a lower bound of zero, like real, dimension(0:n-1) :: a
[toc] | [prev] | [next] | [standalone]
| From | d thiebaud <thiebauddick2@aol.com> |
|---|---|
| Date | 2022-08-27 15:19 -0400 |
| Message-ID | <tedqnk$1414$1@gioia.aioe.org> |
| In reply to | #86117 |
On 8/27/22 01:47, Thomas Koenig wrote: > Lynn McGuire <lynnmcguire5@gmail.com> schrieb: >> “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. > > If you want to declare your Fortran arrays to start at zero, just > declare them with a lower bound of zero, like > > real, dimension(0:n-1) :: a Can you declare other lower bounds the same way?
[toc] | [prev] | [next] | [standalone]
| From | Thomas Koenig <tkoenig@netcologne.de> |
|---|---|
| Date | 2022-08-27 22:22 +0000 |
| Message-ID | <tee5f1$cqm$3@newsreader4.netcologne.de> |
| In reply to | #86122 |
d thiebaud <thiebauddick2@aol.com> schrieb:
> On 8/27/22 01:47, Thomas Koenig wrote:
>> Lynn McGuire <lynnmcguire5@gmail.com> schrieb:
>>> “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.
>>
>> If you want to declare your Fortran arrays to start at zero, just
>> declare them with a lower bound of zero, like
>>
>> real, dimension(0:n-1) :: a
>
> Can you declare other lower bounds the same way?
Yes.
You can, since 1991, when Fortran 90 was released, do
real, dimension(from:to) :: a
where from and to are arbitrary integer expressions. (If to <
from, then you get a zero-sized array, which is perfectly valid
and which just happens to have size zero and no element).
(In Fortran, you cannot declare variables in the middle of
code. If you want to do that, Fortran 2008 introduced the
BLOCK construct for declaring variables, much like
C's or C++'s { and }, so you can do
read (*,*) from, to
block
real, dimension(from:to) :: a
! Use a here
end block
[toc] | [prev] | [next] | [standalone]
| From | "Fred. Zwarts" <F.Zwarts@KVI.nl> |
|---|---|
| Date | 2022-08-27 21:26 +0200 |
| Message-ID | <tedr4l$1cc0$1@gioia.aioe.org> |
| In reply to | #86106 |
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).
[toc] | [prev] | [next] | [standalone]
| From | Lynn McGuire <lynnmcguire5@gmail.com> |
|---|---|
| Date | 2022-08-27 15:01 -0500 |
| Message-ID | <tedt6n$757$1@gioia.aioe.org> |
| In reply to | #86123 |
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
[toc] | [prev] | [next] | [standalone]
| From | Thomas Koenig <tkoenig@netcologne.de> |
|---|---|
| Date | 2022-08-27 22:24 +0000 |
| Message-ID | <tee5i3$cqm$4@newsreader4.netcologne.de> |
| In reply to | #86124 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Lynn McGuire <lynnmcguire5@gmail.com> |
|---|---|
| Date | 2022-08-27 22:40 -0500 |
| Message-ID | <teeo2r$8tl$1@gioia.aioe.org> |
| In reply to | #86126 |
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. 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. I wish that C/C++ would provide some sort of pointer validation but it does not. I keep track of my pointers for that reason and validate them before using when I have some error prone code. In 850,000 lines of F77 and 30,000 lines of C/C++ code, I have suspicious pointers in several areas due to programmers suballocating malloc'd space and forgetting to nullify those suballocated pointers after freeing the allocated space. Old code, you gotta love it. Lynn
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2022-08-27 22:07 -0700 |
| Message-ID | <878rn9j6r4.fsf@nosuchdomain.example.com> |
| In reply to | #86127 |
Lynn McGuire <lynnmcguire5@gmail.com> writes:
> 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.
>>>>
>>>> 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 */
[toc] | [prev] | [next] | [standalone]
| From | Thomas Koenig <tkoenig@netcologne.de> |
|---|---|
| Date | 2022-08-28 07:50 +0000 |
| Message-ID | <tef6nk$1go$1@newsreader4.netcologne.de> |
| In reply to | #86127 |
Lynn McGuire <lynnmcguire5@gmail.com> schrieb: > 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. > Variables can point to anywhere, they are not illegal until referenced. Unfortunately not. Looking at n2596.pdf, one finds under J.2, "Undefined behavior", — Addition or subtraction of a pointer into, or just beyond, an array object and an integer type produces a result that does not point into, or just beyond, the same array object (6.5.6). > Otherwise, code would be crashing all over the place. Undefined behavior does not mean that the code will reliably crash. It just says that the C standard gives no guarantee about what will happen, and, even if it works right now, such code is at the mercy of future compiler revisions, cosmic rays, and other forseen and unforseen circumstances.
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2022-08-28 11:52 +0200 |
| Message-ID | <tefdsv$jj2g$1@dont-email.me> |
| In reply to | #86130 |
On 28/08/2022 09:50, Thomas Koenig wrote: > Lynn McGuire <lynnmcguire5@gmail.com> schrieb: >> 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. >> Variables can point to anywhere, they are not illegal until referenced. > > Unfortunately not. > > Looking at n2596.pdf, one finds under J.2, "Undefined behavior", > > — Addition or subtraction of a pointer into, or just beyond, > an array object and an integer type produces a result that does > not point into, or just beyond, the same array object (6.5.6). > >> Otherwise, code would be crashing all over the place. > > Undefined behavior does not mean that the code will reliably crash. > It just says that the C standard gives no guarantee about what will > happen, and, even if it works right now, such code is at the mercy > of future compiler revisions, cosmic rays, and other forseen and > unforseen circumstances. Indeed. C was designed to have as few restrictions on the hardware as it could, while still having a minimum feature set. If you have a segmented memory architecture (such as x86), then addresses might have a form "segment:offset". If an array starts at offset 0, what does it mean to have an address one before that? It might make no sense, or have different meanings in different contexts, or require inefficient extra instructions to get right. Some architectures pre-load information about memory pages or segments when a pointer register is loaded, causing trouble if it does not actually point to valid memory. Leaving this all undefined is much simpler for everyone. (The other end, one past the end of the array, is too useful in common C idioms to leave undefined - even if it might mean that an implementation can't use the last address in memory.)
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.c++
csiph-web