🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #2175

Re: struct hack, and other out-of-array references

#2175
From: Mark Brader
Date: Thu, 01 Oct 1992 07:03
51 lines
2851 bytes
> > > On the other hand, the various rules on objects, arrays, padding,
> > > etc., do require that a doubly indexed array is also a contiguous
> > > array of its elements.
> >
> > Contiguous, yes; array, no.  If you believe otherwise, please cite
> > something in the standard to support your position.
> 
> Consider the question: what is (char*)&a[0][1] - (char *)&a[0][0]?
> sizeof(&a[0][0]) of course.  Carried out repeatedly, one sees that
> each subelement of a is at the expected offset from the start of
> a.  The only question you might have is: what is (char*)&a[x][0] -
> (char*)&a[x-1][y-1], where y is the number of elements in a[x].
> The rules on sizeof and contiguity of objects require that to be
> sizeof(&a[0][0]) as well.

For ease of discussion I'll assume that a[0][0] has type int.

The above is correct as far as it goes.  And it implies that you can do

	int *p = &a[x-1][y-1];
	p = (int *) ((char *) p + 2 * sizeof (int));

and *p will now be a[x][1].  But you _cannot_ replace the second
statement with a simple "p += 2;" and expect the same result.  This
is the thrust of the Interpretation Ruling that was quoted earlier
in the thread.  You really have to make the excursion into char *
and back:  The standard requires that "a" be treatable as an array
of bytes, and therefore as an array of any character type, but not
as an array of int.

I'll make an analogy again.  If it seems bizarre that "p = (int *)
((char *) p + 2 * sizeof (int));" could be legal where "p += 2;"
is not, consider that "i = (int)(i + (long) j - k);" can be legal
where "i = i + j - k;" would not (e.g. if all 3 variables were
30000 initially and ints are 16 bits).  This is the same sort of
thing -- you have to make a temporary excursion to another type
where the operations you want to carry out have the desired range.

> In other words, these pointers are just pointers into an array,
> and correspond in the expected way to pointers into the doubly
> indexed array.

They point to locations which are located as they would be in a
simple array, but the permitted operations are not the same.
Therefore it is not an array.
-- 
Mark Brader		     "I always pass on good advice.  It's the only thing
SoftQuad Inc., Toronto	      to do with it.  It is never any use to oneself."
utzoo!sq!msb, msb@sq.com 	-- Lord Goring  (Oscar Wilde: An Ideal Husband)

This article is in the public domain.
Technical Details
Message-ID: <1992Oct1.070345.14854@sq.sq.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!uunet.ca!wildcan!sq!msb
References: <Bv2B20.K52@twwells.com> <1992Sep25.012337.22640@sq.sq.com> <BvDnHn.CMK@twwells.com>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!sparky!uunet!uunet.ca!wildcan!sq!msb
From: msb@sq.sq.com (Mark Brader)
Subject: Re: struct hack, and other out-of-array references
Message-ID: <1992Oct1.070345.14854@sq.sq.com>
Organization: SoftQuad Inc., Toronto, Canada
References: <Bv2B20.K52@twwells.com> <1992Sep25.012337.22640@sq.sq.com> <BvDnHn.CMK@twwells.com>
Date: Thu, 1 Oct 92 07:03:45 GMT
Lines: 51