PATCH: BPs & cleanups for ARRAY_SIZE

Raja R Harinath harinath@cs.umn.edu
Wed Aug 23 15:08:00 GMT 2000


Greg McGary <greg@mcgary.org> writes:
> Note that it is possible, through application of extra force, to make
> BPs flatten a multi-dimensional array:
> 
> # define BOUNDED_N(PTR, N) 				\
>   ({ __typeof (PTR) __bounded _p_;			\
>      __ptrvalue _p_ = __ptrlow _p_ = __ptrvalue (PTR);	\
>      __ptrhigh _p_ = __ptrvalue _p_ + (N);		\
>      _p_; })
> 
> int a[10][10];
> int *ap;
> 
> ap = BOUNDED_N ((int *) a, sizeof a / sizeof int);
> 
> ... though I don't recommend the practice without compelling justification.

Why is this necessary?  The compiler already has all the information
for

  ap = (int *) a;

to do what the macro does.  The exact same intent is expressed: I want
to treat a multidimensional array as a single-dimensional array.

Also, I'm not sure the clause 6.5.6 applies here.  That talks about
"if the array is big enough" wrt subscripting, i.e., I cannot refer to
a[9][9] above as a[0][99].

I cannot find anything about the underlying data being accessed
through a different type (int *) rather than (int [10][10]) and then
(int [10]).  It may well be undefined behaviour.  However, if I
understand right, with

  union { int a[10][10]; int b[100]; } c;

GNU C (and most other C compilers) allows both 'a' and 'b' to be
accessed "meaningfully" simultaneously.  The above cast would be an
extension in the same tradition (and probably also works on most
compilers right now).

- Hari
-- 
Raja R Harinath ------------------------------ harinath@cs.umn.edu
"When all else fails, read the instructions."      -- Cahn's Axiom
"Our policy is, when in doubt, do the right thing."   -- Roy L Ash


More information about the Gcc-patches mailing list