
C array syntax proposal (was: Why are most HPC done)
|>
|> >|> Now, how could I best access b as if it were an n*n array?
|> >|>
|> >|> Should it be possible to declare a 'temporary' array, as in
|> >|>
|> >|> double **b;
|> >|> b = malloc(sizeof(double)*n*n);
|> >|> {
|> >|> double b_array[n][n] = b;
|> >|> }
|>
|> >Why? Why invent such an unreadable syntax. (Of course, what is unreadable to
|> >one, is perfectly natural to another.)
|>
|> Because I want to access array elements with constant memory stride.
My suggestion was the currently illegal
double (*b)[n] = malloc(sizeof(double[n]));
Why do you think this involves a variable stride?
(Actually when you write b[i][j] after declaring it b[m][n], it is exactly
the same as
*( (double*) &*( (double(*)[n])&*(b+0) + i) + j)
Where I have put in the redundant casts to show why my syntax is identical to
what you wrote. Also notice that the &* and the +0 are there to enforce the
syntax rules: they involve no computation.
If the above notation is obscure, let me point out that with both
double b[m][n]
and
double (*b)[n]
b[i][j], if valid, is same as *(double*)((char*)b + (i*n+j)*sizeof(double)), if
valid. There is no difference between the two in the striding. I think you
are confusing my suggestion with double **b, which indeed is a different
matter.
|>
|> This has been discussed on c.l.f and c.l.c just recently. The only way to
Nothing to the contrary has been posted in comp.lang.c (I read
comp.lang.fortran sometimes, and haven't seen anything there either.) The
problem is that C does not allow _variable_ dimensioning. That is not at all
the same thing as inventing a new syntax where an array can be initialized
from a pointer (your suggestion). In fact, allowing it, is both unnecessary
(as I pointed out) and without enough care, will make the grammar hopelessly
ambiguous.
|> get the intended effect right now is via
|>
|> b[n*i+j]
|>
|> (which is what f2c does). C definitely lacks a good matrix data type;
there
|> are about 10 ways to get it half right.
The reason, as I said, is absence of the type `double[n]' with n a
non-constant expression: not two dimensional arrays initialized by pointers.
--
Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]