Why does C hate 2d arrays?

Peter da Silva peter at ficc.ferranti.com
Sat May 26 01:14:33 AEST 1990


In article <12923:May2502:17:3090 at stealth.acf.nyu.edu> brnstnd at stealth.acf.nyu.edu (Dan Bernstein) writes:
> In article <K3O3WK1 at xds13.ferranti.com> peter at ficc.ferranti.com (Peter da Silva) writes:
> > [assume: array[m][n] works for m and n not constants]
> > What possible advantage would there be to requiring that m and n not
> > change value while array is in scope?

> The compiler isn't *forced* to make extra, possibly unnecessary, copies
> of m and n.

The compiler is not forced to make extra copies of m and n either way. Only
if they're found to have changed.

> > Since m andd n can be expressions,
> > you are going to want to stash the values away anyway just for the sake
> > of optimisation.

> Not necessarily: m and n may be in registers anyway. To generalize a
> bit: C is a constant, calculated from expression E. We have to know the
> value of C for the lifetime of A. ``Therefore,'' you say, ``we must pick
> an arbitrary time in that lifetime, calculate E then, and stash it away
> in C's special hiding place.''

If you want to guarantee that C is to remain constant during the duration
of E. What about this case:

struct _point { int x, y };

foo(box)
struct point box[2];
{
	char tmp[box[1].x - box[0].x][box[1].y - box[0].y];

	bar(box, tmp);
	print(box, tmp);
}

How do you guarantee that box is constant? In general, you can't. This is
back to the old noalias problem. Better sidestep it completely by just using
the value of box at declaration.

> Do you agree that, given this scenario, it's better for the programmer
> to have access to C's value than not?

How do you feel about:

	char *a = malloc(some_expression);

How does this materially differ from:

	char a[some experssion];

in the context of this discussion?

Do you agree that, given this scenario, it's better for the programmer to
have access to !some expression!'s value than not? Whether you're mallocing
or not.

> In other words, do you agree that
> C should be explicitly declared and initialized by the programmer?

It may be good programming practice, but this is C, not Pascal. C is expected
to bend over backwards to do weird stuff that flakey programmers might want
to do.
-- 
`-_-' Peter da Silva. +1 713 274 5180.  <peter at ficc.ferranti.com>
 'U`  Have you hugged your wolf today?  <peter at sugar.hackercorp.com>
@FIN  Dirty words: Zhghnyyl erphefvir vayvar shapgvbaf.



More information about the Comp.lang.c mailing list