PATCH: BPs & cleanups for ARRAY_SIZE

Geoff Keating geoffk@cygnus.com
Wed Aug 23 13:54:00 GMT 2000


Alexandre Oliva <aoliva@redhat.com> writes:

> On Aug 23, 2000, Greg McGary <greg@mcgary.org> wrote:
> 
> >          -- An  array  subscript is out of range, even if an object
> >             is apparently accessible with the given  subscript  (as
> >             in  the lvalue expression a[1][7] given the declaration
> >             int a[4][5]) (6.5.6).
> 
> Ok, this applies to array subscripts, but does it apply to pointers
> derived from the arrays too?  In particular, does it apply to char*s?

Yes.  (Section 6.5.6 is actually about the additive operators, not about []).

For instance

typedef float t_t;
t_t f[2];
t_t *a = &f[0];
t_t *b = f;
char *ac = (char *)a;
char *bc = (char *)b;
t_t tf;
char tc;
size_t sf = sizeof(f[0]);

tf = f[1];  // OK
tf = a[1];  // undefined behaviour, 'a' points to one float.
tf = b[1];  // OK
tc = ac[sf];  // undefined behaviour
tc = bc[sf]; // OK, bc points to an object of size (sizeof(float)*2).

and all this still holds even if you write

typedef float t_t[10];

or make t_t any other type.

-- 
- Geoffrey Keating <geoffk@cygnus.com>


More information about the Gcc-patches mailing list