zrm (12/17/82)
A blemish on the language C, it is. Can anyone out there think of a reason one would want to return a structure, much less push such a beast on a stack? It makes for extra copying and invites disaster in the from of trying to pass a very large structure, such a whole process (say in a terminal concentrator system...), or some other very large object that might be organised as a structure. One exception I can think of might be a typedef object -- say a Fortranesque complex number. But outside these rare cases, passing structures sound like very bad practice. Cheers, Zig
gwyn@Brl (12/30/82)
From: Doug Gwyn <gwyn@Brl>
Date: 28 Dec 82 14:07:43-EST (Tue)
The main structure I can think of that is worth returning from functions is
struct complex
{
double re;
double im;
};
However, I believe one could always pass a pointer to a caller-allocated
return storage block instead. (I have a complex arithmetic library that
works that way.)
mike@Rand-Unix (12/30/82)
Date: Tuesday, 28 Dec 1982 12:38-PST Why would anyone want to return a structure from a function? Because some functions that operate on non-primitive data types (ie, structures and typedefs) need to return such a structure to the caller. Passing the structure itself instead of an address makes it easier to write recursive code. An example is a graphics system we are developing where a function returns a color and a color is defined by the components red, green blue and matte. Michael Wahrman
dan@Bbn-Unix (12/30/82)
From: Dan Franklin <dan@Bbn-Unix> 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
jab@Okc-Unix (12/30/82)
From: Jeff Bowles <jab@Okc-Unix> Date: 29 Dec 1982 12:59:32 CST (Wednesday) Why would you want to return a structure? Well, returning a FORTRANesque complex number is a pretty good example. Other examples might be: given two objects, return the binary operation using the objects as operands. For example, component-wise addition on two vectors. I, for one, wouldn't like being FORCED to know whether or not something of type "gorp" (from someone else's typedef .* gorp; ) fits into a subroutine return register. Jeff
lee.usc-cse@Udel-Relay (12/30/82)
Date: 28 Dec 1982 11:36-PST Natch, I do it all the time, with typedef-ed things like Fortran complexes (why is this an exception?). It simplifies programs that are characterized by calls like foo(thing.part1,thing.part2,thing.part3) Not only is it simpler, it eliminates the need to decide on a case by case basis which parts of an object have to be passed. And, in fact, it is more efficient on most machines that have block move instructions. - Lee
zemon (01/07/83)
Subject: reason to return structures Date: 7 Jan 1982 I ran across one good reason to return a structure recently. I wrote a routine that returned two separate objects: a number and the address of a routine.
chris.umcp-cs@UDel-Relay (02/28/83)
From: Chris Torek <chris.umcp-cs@UDel-Relay> Date: 31 Dec 82 04:19:01 EST (Fri) Minor point here: structure assignment is more effecient on a Vax than assignment of all of its elements. I haven't noticed how they get pushed onto the stack but I'll bet it's similar. After all this discussion I'm gonna have to look to see how this "returning structures" stuff works (obviously they aren't in R0!).