0

This is my code :

gint compare_ulong_function (gconstpointer a, gconstpointer b)
{
        return *(unsigned long *)a - *(unsigned long *)b;
}

GArray* build_priority_queue(unsigned char* const input_data, unsigned int const input_size)
{
    GArray* priority_queue = g_array_sized_new(FALSE, TRUE, sizeof(unsigned long), 256);

    int i;
    for (i = 0; i < input_size; i++)
            g_array_index(priority_queue, unsigned long, input_data[i])++;
    g_array_sort(priority_queue, compare_ulong_function);

    return priority_queue;

}

When I debug the g_array_sort is called (can't go into the function missing development package and can't install it) but the array isn't sorted. The compare_ulong_function is never called.

Juskie
  • 67
  • 1
  • 12

1 Answers1

2

As far as glib is concerned, you haven't actually added any elements to the array. You just preallocated space for them and then started accessing their memory directly (g_array_index doesn't check whether you're within bounds or not).

g_array_sort still sees the size of the array as 0. (It has space allocated for 256 elements but none have been added).

Try doing a g_array_set_size(input_size) before your loop.