Address of array

Dan Franklin dan at BBN-PROPHET.ARPA
Tue Mar 25 04:52:11 AEST 1986


Bennett E. Todd III (mcnc!ecsvax!duccpc!bet) suggests that structure
copying is a bad idea and wants structures to behave like arrays, instead
of the reverse as everone else is suggesting.

I couldn't disagree more.  Having the compiler "automagically" copy
structures around for me is perhaps the single most useful capability
recently added to C.  It vastly simplifies the problems of dealing with
data structures.

Rob Pike's paper on the organization of the Blit software (in Software
Practice and Experience) demonstrates how clean C programming can be given
structures that can be passed as arguments and returned.  If structures
were only referenced by their addresses, using them would be much more
painful (as it used to be), since every time you returned one of the damn
things you would have to worry about where the storage was coming from.
Here's an example adapted from things in Pike's paper:

    typedef struct { int x, y; } point_t;
    point_t mk_point(int, int);

    rectangle_t output;
    output = mk_rect(mk_point(x,y), mk_point(x + 5, y + 6))

That is, make a rectangle from two points specifying the corners.  Try to
imagine doing this simple operation in a world in which structures are not
copied.  The mk_point routine would either have to take a pointer to an
empty structure to be filled in as one of its arguments, either returning
that same pointer as its return value (so it could be used in cascaded
function calls) or return a pointer to an allocated structure that would
have to be freed later (so I'd have to capture it as it went flying by in
the middle of this expression), or return a pointer to a static structure
that was overwritten with each call (which would make this expression
impossible, since the same pointer would be returned in both cases).  No
matter how you slice it, it would be a lot more than two lines of code,
and not nearly as clear.

	Dan Franklin



More information about the Comp.lang.c mailing list