GNOME Bugzilla – Bug 313127
I replaced qsort of stdlib.h with g_qsort_with_data, and gets SegV.
Last modified: 2005-08-10 16:13:21 UTC
Please describe the problem: To get shuffled natural numbers sequence, I writed a cmp_func which returns [-1, 0,1] randomly and then quick sorted the array of integers. When I used qsort of stdlib.h , it just worked fine, but when replace it with g_qsort_with_data of glib.h, SOMETIMES causes SegV. Steps to reproduce: #include <glib.h> #define A 16 gint gint_cmp_shuffle(const gint *a, const gint *b, gpointer user_data) { return g_random_int_range(-1, 2); } gint main(gint argc, gchar *argv[]) { gint i; gint table[A]; for (i = 0; i < A; i++) table[i] = i; for (i = 0; i < 128; i++) { g_print("%d\n", i); g_qsort_with_data(table, A, sizeof(gint), (GCompareDataFunc) gint_cmp_shuffle, NULL); } return 0; } Actual results: Outputs of gprint are like "0 1 2 ... 17 Segmentation fault" "0 1 2 ... 6 Segmentation fault". Expected results: "0 1 2 ... 126 127" Does this happen every time? Yes. Other information: Following codes works fine, it always outputs "0 1 2 ... 126 127" and ends normally. #include <stdlib.h> #include <glib.h> #define A 16 int int_cmp_shuffle(const int *a, const int *b) { return g_random_int_range(-1, 2); } int main(int argc, char *argv[]) { int i; int table[A]; for (i = 0; i < A; i++) table[i] = i; for (i = 0; i < 128; i++) { g_print("%d\n", i); qsort(table, A, sizeof(int), (int(*)(const void*, const void*)) int_cmp_shuffle); } return 0; }
What makes you think that would work ? the compare function passed to qsort has to implement a (partial) order relation, ie be at least transitive and antisymmetric