bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gnulib] Re: comparison_fn_t


From: Paul Eggert
Subject: Re: [bug-gnulib] Re: comparison_fn_t
Date: Fri, 27 May 2005 17:26:34 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.4 (gnu/linux)

"Oskar Liljeblad" <address@hidden> writes:

> Sure, but assume you're passing strcmp for the comparison function,
> wouldn't you want to cast it to avoid the warning?

I should warn you that the C Standard does not allow that sort of
cast.  This is for portability to hosts that use different
representations for different kinds of pointers; such hosts can use
different calling conventions for char * and void *, so casting the
function pointer will result in code that doesn't work.  Admittedly
such hosts are rare (typically they're word-oriented machines) but
all other things being equal we might as well port to them.

Personally I avoid this problem by using a shim, e.g.:

   static int actual_compare (my_type *a, my_type *b) { ... whatever ... }
   static int cmp (void *a, void *b) { return actual_compare (a, b); }
   ...
   qsort (array, nitems, sizeof *array, cmp);

This avoids the need for casts entirely (and will work on that old
Unisys or Cray mainframe :-).

Admittedly there's a performance hit in some cases but it's typically
so small I can't measure it.  And making "actual_compare" inline
shrinks the runtime overhead to zero.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]