Function pointer casts



Hello,

First:
I am reading the GPtrArray code (in glib/glib/garray.c) from Git.  I noticed this code in g_ptr_array_sort():

void
g_ptr_array_sort (GPtrArray    *array,
                  GCompareFunc  compare_func)
{
  g_return_if_fail (array != NULL);

  /* Don't use qsort as we want a guaranteed stable sort */
  g_qsort_with_data (array->pdata,
                     array->len,
                     sizeof (gpointer),
                     (GCompareDataFunc)compare_func,
                     NULL);
}

This code casts GCompareFunc to GCompareDataFunc.  As I understand, casting function pointers in C is not allowed.
Ref: http://stackoverflow.com/questions/559581/casting-a-function-pointer-to-another-type

I am happy to post a patch if others are in agreement.  However, since I am new to hacking GLib & GTK+, perhaps there are good historical/technical reasons to cast function pointers as above.  If so, please explain.

Second
I would like to add a test if compare_func is NULL.  This seems strange that we allow compare_func to be NULL here.  Again, if this is a bad idea, please tell me.  (Same also for g_ptr_array_sort_with_data().)

Third
I checked the g_qsort_with_data() code.  There is also no check if compare_func is NULL -- or any other params

Thanks,
Arpe
P.S. I just realised today that I have been incorrectly sending dev-style mails to gtk-list gnome org.  Apologies if I annoyed anyone(!).



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