Why pass structs? (not struct pointers)

guy at gorodish.UUCP guy at gorodish.UUCP
Sat Feb 28 05:44:56 AEST 1987


>Passing structures as a whole seems to violate my impression
>of the nature of C.  I feel that a good reason for passing
>arrays by reference (yes, I know that the pointer is passed by
>value :-) is to save an enormous ammount of stack and CPU time.

Yes, but the mere fact that something takes a lot of CPU time or
stack space isn't sufficient to indicate that C shouldn't do it; some
people, for instance, might argue that C implementations on the VAX
shouldn't use "calls", because they take a lot of time :-).

>To pass a structure by value seems to require an arbitrarily
>large amount of stack; but more importantly, non-atomic
>stack operations.  That is, that I can push a long, int, char
>or any pointer with (hopefully) one machine-op.

Well, you can't.  For instance, on the machine on which C was
originally implemented, you *can't* push a long with one machine
instruction.  Again, "can I do this in a machine instruction" is the
wrong question, as is "is this operation atomic".  (Remember, the
instruction to push such an item may not be atomic, even if it is
done in one machine instruction; what if it takes a page fault when
doing the push, or gets a segmentation violation because the stack
hasn't grown far enough yet?)

>I imagine that the people who added this to C had good reason,
>so could someone please tell me what I am missing or have
>gotten wrong?

According to Dennis Ritchie (at least as far as I remember his answer to
this question), the reason why structure assignment, structure-valued
arguments, and structure-valued functions were added was to make C
treat all scalar objects in a similar fashion.



More information about the Comp.lang.c mailing list