Article View: comp.std.c
Article #2161Re: struct hack, and other out-of-array references
From: T. William Wells
Date: Wed, 30 Sep 1992 06:03
Date: Wed, 30 Sep 1992 06:03
39 lines
2306 bytes
2306 bytes
In article <1992Sep25.012337.22640@sq.sq.com> msb@sq.sq.com (Mark Brader) writes:
: > 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.
Way back when, I noted that there was an unusual dichotomy between
object addresses and addresses in objects. Object addresses (of
objects not contained in objects) are completely independent of
one another. One can't assume anything about these except that
pointers to distinct objects (counting null as an "object") don't
compare equal. However, addresses within objects behave as if
they were in an ordinary flat address space. For example,
(char*)a[n] op (char*)a[m], (op is any relational) has the same
truth value as n op m. I asked about this during one of the
public comment periods and was told that it was intentional that
addresses within object behaved this way. (Similarly, if two
fields in a structure have a particular ordering, so do their
offsets.)
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.
In other words, these pointers are just pointers into an array,
and correspond in the expected way to pointers into the doubly
indexed array.
Which is just a fancy way of saying that it is an array.
---
Bill { rutgers | decwrl | telesci }!twwells!bill
bill@twwells.com
Technical Details
Message-ID:
<BvDnHn.CMK@twwells.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!rutgers!twwells!billReferences:
<1992Sep16.232750.21829@sq.sq.com> <Bv2B20.K52@twwells.com> <1992Sep25.012337.22640@sq.sq.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!rutgers!twwells!bill From: bill@twwells.com (T. William Wells) Newsgroups: comp.std.c Subject: Re: struct hack, and other out-of-array references Message-ID: <BvDnHn.CMK@twwells.com> Date: 30 Sep 92 06:03:16 GMT References: <1992Sep16.232750.21829@sq.sq.com> <Bv2B20.K52@twwells.com> <1992Sep25.012337.22640@sq.sq.com> Organization: None, Mt. Laurel, NJ Lines: 39