Reply by Chris Bore October 22, 20042004-10-22
It is amazing what you people know!

Is this a perhaps very good example of why a programmer should not try
to produce code 'knowing' what the compiler will make of it?

Chris
===============================
Chris Bore
chris@bores.com


Jerry Avins <jya@ieee.org> wrote in message news:<2tj2fiF1vgt41U1@uni-berlin.de>...
> Randy Yates wrote: > > Jerry Avins <jya@ieee.org> writes: > > > > > >>Randy Yates wrote: > >> > >> > >>>Jerry Avins <jya@ieee.org> writes: > >>> > >>> > >>>>[...] > >>>>Also evident is that > >>>>for the reordering to work, the compiler must retain those associations > >>>>even when the objects are out of their expected places. Dirk showed that > >>>>at least one compilers doesn't work that way. > >>> > >>> > >>>He did? Where, Jerry? Maybe my newsserver missed that post. > >> > >>"I put it in my C++ compiler (Borland C++ Builder), it did compile, but > >>printed meaningless numbers." > > > > > > Perhaps the point of confusion here is that my original code snippet > > was meant to convey the simple fact that such a construction compiles > > without error. It does not convey that a[i] = i[a], even though it > > does. In a subsequent post I gave code and a makefile which showed > > that i[a] both compiles and is equivalent to a[i]. > > > > Even with the original snippet, Dirk did NOT show that "at least one > > compilers [sic] doesn't work that way" since that code does not allow > > that determination to be made one way or the other. > > Agreed. It would have to have been done both ways, either in the same > compilation or in two identical except in that one respect. > > Jerry
Reply by October 22, 20042004-10-22
Randy Yates <randy.yates@sonyericsson.com> writes:

> I owe you an apology, Martin. I interpreted ill-will where there was > none, and in the process attacked you. I apologize. I was wrong in > the motive and substance of my attack. You're right, you did not > miss anything. >
No probs - I was only concerned I wasn't missing anything I could be learning! Cheers, Martin -- martin.j.thompson@trw.com TRW Conekt, Solihull, UK http://www.trw.com/conekt
Reply by October 21, 20042004-10-21
I owe you an apology, Martin. I interpreted ill-will where there was
none, and in the process attacked you. I apologize. I was wrong in
the motive and substance of my attack. You're right, you did not
miss anything. 

--Randy


Martin Thompson <martin.j.thompson@trw.com> writes:

> Randy Yates <randy.yates@sonyericsson.com> writes: > > > Martin Thompson <martin.j.thompson@trw.com> writes: > > > > Martin Thompson <martin.j.thompson@trw.com> writes: > > > > > Randy Yates <yates@ieee.org> writes: > > > > > > That's the point - they're isomorphic. > > > > > > > > > > Which means you can write comedy code like 5[a] instead of a[5] - > > > > > that'll give you real job security :-) > > > > > > > > That you miss the syntactic relevence does not bode well for yours. > > > > > > 'scuse my dimness, but the "syntactic relevance" of what have I > > > missed? > > > > That there is no such thing as an array reference in the internals of the > > compiler. The syntactic element a[i] is first converted to *(a+i), then > > the compiler operates on *(a+i) in the usual manner. > > I hadn't missed that, that was the point of my "5[a]" post - you can write it > either way around, even with a constant as (what looks like) the "array > name". > > Cheers, > Martin > > -- > martin.j.thompson@trw.com > TRW Conekt, Solihull, UK > http://www.trw.com/conekt
-- Randy Yates Sony Ericsson Mobile Communications Research Triangle Park, NC, USA randy.yates@sonyericsson.com, 919-472-1124
Reply by October 21, 20042004-10-21
Randy Yates <randy.yates@sonyericsson.com> writes:

> Martin Thompson <martin.j.thompson@trw.com> writes: > > > Martin Thompson <martin.j.thompson@trw.com> writes: > > > > Randy Yates <yates@ieee.org> writes: > > > > > That's the point - they're isomorphic. > > > > > > > > Which means you can write comedy code like 5[a] instead of a[5] - > > > > that'll give you real job security :-) > > > > > > That you miss the syntactic relevence does not bode well for yours. > > > > 'scuse my dimness, but the "syntactic relevance" of what have I > > missed? > > That there is no such thing as an array reference in the internals of the > compiler. The syntactic element a[i] is first converted to *(a+i), then > the compiler operates on *(a+i) in the usual manner.
I hadn't missed that, that was the point of my "5[a]" post - you can write it either way around, even with a constant as (what looks like) the "array name". Cheers, Martin -- martin.j.thompson@trw.com TRW Conekt, Solihull, UK http://www.trw.com/conekt
Reply by Randy Yates October 20, 20042004-10-20
Tim Wescott <tim@wescottnospamdesign.com> writes:

> glen herrmannsfeldt wrote: > >> Tim Wescott wrote: >> (snip) >> >>> int * column[10]; >>> >>> for (int n = 0; n < 10; n++) column[n] = malloc(sizeof(int) * 10); >>> >>> printf("%d", column[8][3]); >>> >>> -- non-contiguous storage, addressed like an array. >> How about: >> #include <stdio.h> >> #include <stdlib.h> >> int main() { >> int (*a)[10]; >> int i,j; >> a=malloc(100*sizeof(int)); >> for(i=0;i<10;i++) for(j=0;j<10;j++) a[i][j]=i+j; >> printf("%d\n",a[5][5]); >> } >> contiguous storage, dynamically allocated, and addressed like >> a 2D array. >> -- glen >> > Undoubtedly. I was only pointing out that it's not _necessary_.
Yes, I saw your point, Tim, and I agree. That this can so easily be done is one of the neat things about C. -- % Randy Yates % "With time with what you've learned, %% Fuquay-Varina, NC % they'll kiss the ground you walk %%% 919-577-9882 % upon." %%%% <yates@ieee.org> % '21st Century Man', *Time*, ELO http://home.earthlink.net/~yatescr
Reply by Andrew Reilly October 20, 20042004-10-20
On Wed, 20 Oct 2004 16:18:11 -0400, Randy Yates wrote:
> To save a post/response cycle, let me attempt to clarify: > > Randy Yates <randy.yates@sonyericsson.com> writes: > >> there is no such thing as an array reference in the internals of the compiler > > What I meant by "an array reference" is a *string* of the form > "a[n]". I was not saying that the compiler cannot perform the > *function* of referencing elements in an array.
Aah. As expected, we've both been using the words to mean different things. Sorry about that. What I was saying was that all memory references are array references to the compiler, (by virtue of the scaling), however they're expressed in the source code. That's by way of comparison to the syntax of most assemblers and some other low-level languages, where expressions are just integer expressions, pointers are just integers, and any scaling you have to do for yourself, explicitly. To cover glen's point about two(multi)-dimensional arrays: yes, the compiler can do those, where it can know that the "type" that defines each of the scaling (indexing) operators is either the base type or "fixed-size-array of base type". It's still a *(*(typeof(*a)))((char*)a + sizeof(*a) * i) exercise. So, not entirely a trivial point, I hope, but indeed pretty trivial. -- Andrew
Reply by Tim Wescott October 20, 20042004-10-20
glen herrmannsfeldt wrote:

> > > Tim Wescott wrote: > > (snip) > >> int * column[10]; >> >> for (int n = 0; n < 10; n++) column[n] = malloc(sizeof(int) * 10); >> >> printf("%d", column[8][3]); >> >> -- non-contiguous storage, addressed like an array. > > > How about: > > #include <stdio.h> > #include <stdlib.h> > > int main() { > int (*a)[10]; > int i,j; > > a=malloc(100*sizeof(int)); > for(i=0;i<10;i++) for(j=0;j<10;j++) a[i][j]=i+j; > printf("%d\n",a[5][5]); > } > > contiguous storage, dynamically allocated, and addressed like > a 2D array. > > -- glen >
Undoubtedly. I was only pointing out that it's not _necessary_. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
Reply by glen herrmannsfeldt October 20, 20042004-10-20

Tim Wescott wrote:

(snip)

> int * column[10]; > > for (int n = 0; n < 10; n++) column[n] = malloc(sizeof(int) * 10); > > printf("%d", column[8][3]); > > -- non-contiguous storage, addressed like an array.
Here is an array or pointers to arrays, where each array element addresses 100 contiguous ints, stored as 2D arrays. It is then stored into like a 3D array, though I don't know the words to describe the way the data is read out again for the printf call. #include <stdio.h> #include <stdlib.h> int main() { int (*a[10])[10]; /* array of pointers to arrays */ int i,j,k; for(i=0;i<10;i++) i[a]=malloc(100*sizeof(int)); for(i=0;i<10;i++) { for(j=0;j<10;j++) for(k=0;k<10;k++) a[i][j][k]=i+j+k; } for(i=0;i<10;i++) { for(j=0;j<10;j++) printf("%3d",i[j[i[a]]]); printf("\n"); } } Note that i[j[i[a]]] is equivalent to a[i][j][i] through the appropriate conversion between [] and *() operations, either in the case of arrays or pointers. -- glen
Reply by glen herrmannsfeldt October 20, 20042004-10-20

Tim Wescott wrote:

(snip)

> int * column[10]; > > for (int n = 0; n < 10; n++) column[n] = malloc(sizeof(int) * 10); > > printf("%d", column[8][3]); > > -- non-contiguous storage, addressed like an array.
How about: #include <stdio.h> #include <stdlib.h> int main() { int (*a)[10]; int i,j; a=malloc(100*sizeof(int)); for(i=0;i<10;i++) for(j=0;j<10;j++) a[i][j]=i+j; printf("%d\n",a[5][5]); } contiguous storage, dynamically allocated, and addressed like a 2D array. -- glen
Reply by Tim Wescott October 20, 20042004-10-20
glen herrmannsfeldt wrote:

> > > Randy Yates wrote: > > (snip) > >> What I meant by "an array reference" is a *string* of the form >> "a[n]". I was not saying that the compiler cannot perform the >> *function* of referencing elements in an array. > > > There is an argument that says that there are no two-dimensional > arrays in C, only arrays of arrays, which is probably true in the > same sense that your comment is true. > > But what looks like a two dimensional array must be allocated > in contiguous memory, which would not be required for arrays > of arrays, so I believe they can legitimately be called > 2D arrays (or more). > > In any case, given the right declaration the compiler > can figure out how to access the different ways of allocating > arrays. > > -- glen >
int * column[10]; for (int n = 0; n < 10; n++) column[n] = malloc(sizeof(int) * 10); printf("%d", column[8][3]); -- non-contiguous storage, addressed like an array. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com