C array syntax proposal (was: Why are most HPC done)
Author Message
C array syntax proposal (was: Why are most HPC done)

Even if it were possible to pass arrays in C, this still would not
fill all needs.

Consider the case where a routine needs auxiliary storage.  In
fortran 77, you'd probably pass it along with the other arguments;
in Fortran 90, you'd use an ALLOCATABLE array.

The typical C way of doing things would be to allocate memory
with malloc() and then use this, as in:

void foo(double *a, const int n)
{
double *b;
b = malloc(sizeof(*b)*n*n);
[... use b]

Quote:
}

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;
}
?
--

The joy of engineering is to find a straight line on a double
logarithmic diagram.

Tue, 02 Dec 1997 03:00:00 GMT
C array syntax proposal (was: Why are most HPC done)

<snip>
|> 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.)

As long as you _are_ defining arrays with non-constant dimensions, you might
as well choose the obvious

double (*b)[n] = malloc(n*sizeof(double[n]));

/* Allocate space for n arrays of n doubles and assign the pointer to b
after converting to the correct type: the correct type being a pointer to
an array of n doubles */

which is almost exactly like double b[n][n] except for bounds checking: but
then C traditionally offers no help to a compiler which wants to do bounds
checking on a malloc'ed array anyway.

As an aside, a smart compiler should also notice that b has been malloc'ed
and so *(b+expr) cannot be an alias for (expr_not_involving_b) till any
pointer is assigned to from b. (malloc cannot legally be the name of a user
routine). I do not remember seeing a compiler that does this kind of
optimization though.

Cheers
Tanmoy
--

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 ]

Tue, 02 Dec 1997 03:00:00 GMT
C array syntax proposal (was: Why are most HPC done)

Quote:

>|> 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.

This has been discussed on c.l.f and c.l.c just recently.  The only way to
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 joy of engineering is to find a straight line on a double
logarithmic diagram.

Tue, 02 Dec 1997 03:00:00 GMT
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 ]

Tue, 02 Dec 1997 03:00:00 GMT
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.

: This has been discussed on c.l.f and c.l.c just recently.  The only way to
: 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.

Perhaps the solution is to finally bite the bullet and NOT change or mix C's
existing pointer notation and semantics with the *completely new array type
that you will invent*.

Render unto C'sar that which is C'sar's ...... :-)

mbk

Wed, 03 Dec 1997 03:00:00 GMT
C array syntax proposal (was: Why are most HPC done)
...
> Perhaps the solution is to finally bite the bullet and NOT change or mix C's
> existing pointer notation and semantics with the *completely new array type
> that you will invent*.
>
I think Tanmoy's suggestion was very sensible and would give what is wanted
with the least amount of change (and no incompatible change too).
--
dik t. winter, cwi, kruislaan 413, 1098 sj  amsterdam, nederland, +31205924098

Wed, 03 Dec 1997 03:00:00 GMT
C array syntax proposal (was: Why are most HPC done)

:Even if it were possible to pass arrays in C, this still would not
:fill all needs.
:
:Consider the case where a routine needs auxiliary storage.  In
:Fortran 77, you'd probably pass it along with the other arguments;
:in Fortran 90, you'd use an ALLOCATABLE array.
:
:The typical C way of doing things would be to allocate memory
:with malloc() and then use this, as in:
:
:void foo(double *a, const int n)
:{
:    double *b;
:    b = malloc(sizeof(*b)*n*n);
:    [... use b]
:}
:
: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;
:    }

Answer 1: for 2d arrays in C it's best to do the index calculation
yourself (unfortunately).

double *b;
b = malloc(sizeof(*b) * n * n;
#define B(i, j) b[(i) * n + (j)]
/* use B(i, j) */
free(b);

vector<double> b(n);
b(i, j) = ...; // I use ( ) syntax for matrixes as it's easier to implement
// and fortran types can read it easier.  You can also implement
// [][]

--
---
Tim Hollebeek                   'There will be a better sig when I have time'

Wed, 03 Dec 1997 03:00:00 GMT

 Page 1 of 1 [ 7 post ]

Relevant Pages