[comp.lang.c] Return of arrays and structures,...efficiency/clarity/consi

ts@cup.portal.com (Tim W Smith) (04/18/91)

If your array is a constant size, you can pass it using structure
passing:

	int	array[100];
	struct arraystruct
	{
		int array[100];
	};
	...
	func( *(struct arraystruct *)(&array[0]) );

Structure passing does not work by reference.  It is by
value.

A copy of the structure is passed.  This can be very slow on
big structures.  It can also take a lot of code space.  I've
seen compilers that would emit 250,000 move instructions to
pass 1,000,000 byte structure.

The big hassle in structure passing and return is figuring
out when it is safe.  Some compilers, for example, use a
static union of all the structure types that are passed
or returned in the program, and a function returns a
structure by copying it to this static union, where the
caller then copies it to wherever it is supposed to
go (or something like this).

Note that this dies bigtime should an interrupt routine
try to return a structure and the interrupt happened
to interrupt someone who was returning a structure.

You are probably in general best limiting structure
passing and return to small structures, say two or
three integers.

Finally, if you are going to use structure passing or
return, try a few test cases before writing a lot of
code!  This is one of the least used areas of C, and
so seems to get the least amount of testing when a
compiler is developed.

For example, suppose foo() returns a structure.  Some
compilers might not like foo().spam in an expression.
It would not be pleasant to use this construct 100 times
in a large program, and then have to go and rewrite it!

					Tim Smith