From: utzoo!decvax!harpo!seismo!hao!hplabs!sri-unix!dan@Bbn-Unix
Newsgroups: net.unix-wizards
Title: Re: Returning structures
Article-I.D.: sri-unix.4984
Posted: Thu Dec 30 13:48:25 1982
Received: Sat Jan  1 02:09:17 1983

From: Dan Franklin 
Date: 28 Dec 1982 14:13:23 EST (Tuesday)
Passing a structure is very useful if you program by defining data objects and
routines that work with them (usually called object-oriented programming).
Typically the objects will be defined as structures, and the ability to pass and
return them means that you can deal with your own defined objects just as
easily--and with the same syntax--as you deal with the objects that the language
provides.  Complex numbers are only one example.  Since such structures are
typically small, the extra copying is not a problem.  Returning structures is
particularly useful because of the perennial problem of finding storage for the
structure if you just return a pointer; you can either use static storage
declared within the routine, in which case the caller has to know that and be
careful not to use the same routine twice within the same statement, or you can
allocate it, in which case the caller has to know about it and free it. 

The only problem it has is that now, if a routine expects a structure pointer
and you accidentally pass a structure instead, the error is not caught at
compile time; the program runs, with results that can be very confusing.  But
this is not a problem with structure passing; the same problem occurs when
trying to use lseek with an integer offset, etc.  It's a general problem that
ought to be solved by permitting the user to specify that a routine expects
parameters of certain types, and expecting the compiler to check--and where
possible, convert--the actual parameters.

Far from being a blemish, the new structure operations are a powerful and
useful extension to the language. 

	Dan Franklin