(Richard Harter) writes:
<snip>
> The big one is that if we have an array of structs
> we can readily form a pointer to a particular struct whereas with a
> struct of arrays we can't. With an array of structs we can do things
> like
>
> func(a[i]) /* pass a pointer to a struct to a function */
> b = a[i]; /* Make a copy of a struct */
>
> With a struct of arrays we can't. The issue here is that our data in
> effect is a two dimensional array with one of the dimensions being the
> fields. In some languages slicing is intrinsic to the language; in C
> it is not.
<snip>
> The syntax favors one choice over another; i.e., it is
> easier to do certain common things (examples above) with one choice
> than the other.
Part of the debate may be caused by your talking of the *syntax*
favouring one rather than the other. Whilst it is true that there is no
syntax for the "sliced" struct access, nor for a pointer to such a
"sliced" struct, the bias (if that is what it is) goes much deeper than
C's syntax.
The purpose of transposing the data structure is to get a different
layout in memory, so, inevitably, the sliced structs in the "struct of
arrays" layout are not objects in the C sense. All the numerous rules
about object representations, sizes, effective types, pointers to
objects and so on can't apply to these. In other words, the syntax of
the language merely represents its fundamental, low-level, view of
objects, types and values.
Of course, these slices need not be objects -- an enhanced C could
introduce a new kind of pointer and a new kind of "thing" to which these
point, but, again, that's a lot more than removing a bias in the syntax.
I accept that it's possible to view all these changes as simply the
consequence of having new syntax that supports slices or transposed data
structures, but, because of C's low level nature and its history of
having very informally defined semantics, most "C people" (I count
myself as one of these) are used to thinking of the underlying concepts
as fundamental, and of the syntax as being a natural (and rather trivial)
consequence of these.
By the way, in such a language one would want to avoid having to repeat
the size (as has been done in the various examples) -- maybe by just
having a "transpose" keyword on a type declaration or an object
definition. That way, such objects could be naturally malloc'd.
<snip>
--
Ben.