struct ? why
Brian Matthews
blm at cxsea.UUCP
Wed Oct 19 08:52:48 AEST 1988
Lazer Danzinger (lazer at mnetor.UUCP) writes:
|Eric S. Raymond writes:
|>In article <315 at hrc.uucp>, dan at hrc.UUCP (Dan Troxel VP) writes:
|>>Could some of you please give reasons why 'struct'ing variables should be used
|>>in 'C' programming? What speed increases are noticed if any? Code size at end
|>>of compile smaller or larger? Things like that.
|>Structs are provided not for efficiency's sake but in order to permit you to
|>logically group together data that the program will use together.
|>it leads to more readable code . . .
| While I concur that the proper usage of structures leads to more
| readable code, there may indeed be an efficiency related aspect to
| its usage as well (in certain applications) which should not be
| overlooked.
| Consider the following two functions:
|bool update_employee_rec(name, address, salary, id_no, height, weight, etc)
|char *name;
|char *address;
|float *salary;
|int *id_no;
|float *height;
|float *weight;
|bool update_employee_rec(employee_rec)
|Employee *employee_rec;
Two problems:
1. The functions aren't equivalent. Someone calling the first one knows
that name, address, etc. can't be modified by update_employee_rec, as it
gets its own copy of everything. Someone calling the second function has
no such guarantee. This difference may be more important than any
minimal performance improvement gained.
2. While CALLING the second function may be faster, accessing name and
address and such in it may be slower. Accessing name in the first
function (on your run-of-the-mill CISC processor) will be something like
move offset(frame pointer), somewhere
In the second function, it's
move offset(frame pointer), temp addr reg
move offset(temp addr reg), somewhere
This will be ameliorated somewhat by placing employee_rec in a register
(assuming you have enough registers available, etc.), but a good compiler
won't stack and unstack all of the arguments for the first function
either, so you can't immediately assume that using the second function
will result in a performance improvement.
--
Brian L. Matthews blm at cxsea.UUCP ...{mnetor,uw-beaver!ssc-vax}!cxsea!blm
+1 206 251 6811 Computer X Inc. - a division of Motorola New Enterprises
More information about the Comp.lang.c
mailing list