Just a minor new twist on free()

Gerhard Gucher gegu at bii.UUCP
Fri Oct 5 06:03:24 AEST 1990


> In article <PDS.90Oct3103126 at lemming.webo.dg.com>, pds at lemming.webo.dg.com (Paul D. Smith) writes:

>     #define ARRAY_LEN   10000
>     char *array[ARRAY_LEN], *ap;
>     int i;
> 
>     for (ap = array, i = 0; i < ARRAY_LEN; ++i, ++ap)
>     {
>         if (ap != NULL)
>             free(ap)
>     }
> 
> Now, *this* is a significant performance hit, if you consider an extra
> 10000 comparisons.  Before you retort about using memset() or bzero(),
> please read the FAQ on NULL pointers ... and no, I wouldn't do it this
> way either, but it is not hard to see where extraneous tests *can*
> cause some loss of performance, not to mention loss of programmer time
> typing them in!
> 
> Besides, I think it is correct behavior for free() to handle a NULL
> pointer, and it should have done all along.
> 
> 
>> In addition, by adding the if() you get code that is portable
>> across all implementations.
> 
> Ok, now *this* is a valid concern.  So, rewrite the macro in question:


Let's think a bit about "this significant performance hit".  

case 1:		ap == NULL  for > 90% of all cases

    WE GAIN by saving more than 9000 dummy function calls !! 

case 2:		ap == NULL  for 10% ... 90% of all cases

    WE GAIN by trading function calls for if's ( if's are MUCH cheaper )

case 3:		ap == NULL  for < 10% of all cases 

    WE LOOSE VERY LITTLE because we can't measure >9000 if's compared
                         to those >9000 fully executed frees. 

Now again, how was that with that *significant* performance hit ??

And as we all agree (I hope), the gain of portability to non ANSI
systems is well worth it anyways. (Keep in mind, most in use systems
don't carry ANSI yet ( Mess - DOS doesn't count as a system (:-) ).

--
  Disclaimer: My employer has nothing to to with this.
--

+------------------------------------------------------------------+
| Gerry Gucher    uunet!bii!gegu       gegu at bii.bruker.com	   |
| Bruker Instruments Inc. Manning Park Billerica MA 01821          | 
+------------------------------------------------------------------+



More information about the Comp.lang.c mailing list