Portability of passing/operating on structures...
Bryan Dawson ext 1184
dawson at fpssun.fps.com
Fri Oct 21 01:05:17 AEST 1988
In article <2685 at hound.UUCP>, rkl1 at hound.UUCP (K.LAUX) writes:
> In article <8810111934.AA21941 at ucbarpa.Berkeley.EDU>, U23405 at UICVM.Berkeley.EDU (Michael J. Steiner) writes:
> > What I would like to know is (someone asked this before, I think):
> >
> > Is it considered portable to do the following things with
> > structures or unions?
> > -pass them (by value) to functions
> > -have functions which return them
> > -assign them (=)
> > -test them (structures only) for equality with ==
> >
> > Thanks in advance,
> >
> > Michael Steiner
> > Email: U23405 at UICVM.BITNET
>
>
> *Why* would you want to do this anyway? It is far easier to use
> pointers (and much more efficient). You should pass a *pointer* to the
> struct/union, declare functions as *returning a pointer* to the struct/union,
[ lots of good arguments for using pointers to structs deleted... ]
Well, since you *did* ask, there are many very useful algorithms
for sorting, searching, building, manipulating and accessing data
structures which are *much* easier to use if the atomic 'data
item' is passed into a functions *by value* and if functions
can also generate *and then return* items of this type. For
practical applications, this atomic 'data type' will usually
be a structure with several fields. Some of these algorithms
are recursive and require a 'new' instance of the structure
on every call. This is done much more easily and naturally
by passing the prior structure in *by value* modifying the
new version and continuing.
It is probably for good reasons like this that most good
C compilers and the ANSI draft standard allow structures to
be passed by value, returned from functions, and assigned as
entities. Test for equality is prohibited by ANSI and seldom
supported because it is troublesome to implement when the
structures in question are padded internally and thus have
undefined 'holes' within them which preclude a byte-for-byte
test for equality.
Please note that I do not disagree that pointers to
structures are very natural and useful and can often be a
more efficient method for manipulating structures with
functions. I am merely pointing out that the use of
structures as full-fledged entities sometimes has value
which outwieghs the efficiency penalty for such use.
It is sometimes important to note (especially in C) that
efficiency is only ONE of MANY constraints on good program
design. In situations where efficiency is not critical,
I'll take readable, maintainable, simple and natural
code which clearly follows the chosen algorithm any day...
--
============================================================================
= Bryan Dawson tektronix!fpssun!dawson ...The story you have just =
= read is true, reality was subsequently changed to protect the innocent. =
============================================================================
More information about the Comp.lang.c
mailing list