struct ? why

Lazer Danzinger lazer at mnetor.UUCP
Tue Oct 18 06:48:15 AEST 1988


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;

    In the former format, the compiler will have to generate code to
    push many variable addresses on the stack (each time the function 
    appears in the code).

    In the latter format, the compiler has to generate code to push
    a single address on the stack (the address of the structure).
    (This results in a smaller object and executable size.)

    Naturally, at run-time, a function coded in the latter format 
    will run (much) faster as well, since there is a minimum of pushing
    and popping off the stack. 

    Of course the measure of efficiency obtained depends on the number
    of times a function is invoked, and how many parameters the function
    would otherwise have.

    One should be careful against the abuse of stuctures, though.
    I have frequently seen code where a pointer to a structure is passed 
    to a function, but only 2 or 3 members of the structure are
    referenced in the function, out of the _many_ members belonging to 
    the structure. This is definitely poor programming. It obscures
    the interface, and makes the code more difficult to re-use.
    Another similar danger is the temptation to throw just about everything,
    short of the kitchen-sink, into a single structure. Why do some
    programmers do this? 
    So that they can pass a single parameter in the functions that 
    they write, of course! 

-- 
-----------------------------------------------------------------------------
Lazer Danzinger      | "Ben Zoma said, 'Who is wise?  One who learns from 
lazer at mnetor         | every  person...Who is courageous?  One who conquers 
                     | his [evil] inclination...Who is wealthy?  One who is 
                     | satisfied with his portion...Who is respected?  One who 
(416) 475-8980       |  respects his fellow man...'"         -- Avot 4:1
----------------------------------------------------------------------------




More information about the Comp.lang.c mailing list