Address of array; really structure & [array] passing

KW Heuer kwh at bentley.UUCP
Mon Apr 7 05:36:04 AEST 1986


In article <456 at cubsvax.UUCP> cubsvax!peters (Peter S. Shenkin) writes:
>A naive question.  When a structure ... is passed [by value], doesn't
>everything in it have to be copied at execution time ...?  Wouldn't this
>also have to occur if arrays could be passed by value?
>If the structures/arrays are large, doesn't this incur a large overhead?

Yes, though it's partially reduced if the architecture has a fast block-
move instruction.

>That is, it seems to be a tradeoff between programming ease and syntactical
>elegance, on the one hand, versus time as well as space, on the other.
>I'm not trying to make a general argument against pass-by-value for structures
>[or arrays], but isn't the practice best avoided when large structures may
>be expected?

Indeed, most call-by-value and return-by-value structures are small (complex
numbers, coordinates of a point, etc.).  Almost all structures are passed by
reference.  Presumably if arrays had been implemented "correctly", programs
would still pass them by reference ("f(&a)" or "f(&a[0])") except in special
cases.

>On rereading this, perhaps I'm wrong;  if I declare
>	 struct {  float array[MANY];  } sarray;
>and call 
>	 result = func(sarray);
>are all the elements of sarray.array passed to func(), or is just the
>address of the array copied when the structure is passed?  Enlighten me!

The entire structure is copied, including all the elements of the array.
This is the standard kludge for passing an array or a float by value (e.g.
to a routine written in another language).



More information about the Comp.lang.c mailing list