Re: g_list_insert_sorted bug




Some Guy <guy@mikepery.pr.mcs.net> writes:

> On Wed, 1 Jul 1998, Ian McKellar wrote:
> 
> > After breif experimentation, the broblem seems to be a bit more widespread. It
> > seems that there is a NULL item in every GList. g_list_foreach calls my
> > function with NULL too.
> 
> Yes, the way GList is implemented, each list starts off with a NULL first
> item.  This is not necessarily a bug, it's just part of g_list's algorithm.  
> There are a couple of ways to get around this:
>   1. Since the first item is always NULL, use g_list_prepend instead of
>      _append.  This will move the NULL to the end of the list so you don't
>      have to deal with it (it can be an 'end of list' type marker).
>   2. Whenever you have a g_list_foreach callback, just test for null and
>      skip past that item if it is null.
> There are list algorithms that utilize the whole list without placing a
> NULL in the first item, but apparently glib doesn't implement it that way.

No, no, no. This is entirely wrong.

An _empty_ GList is represented by NULL. This is entirely different
from a list node with the data member equal to NULL.

 GList *list = NULL;       /* empty */
  
 list = g_list_append (list, "one"); /* now has one element */
 list = g_list_append (list, "two"); /* now has two elements */

At the end of this, we have:

list => element #1

element #1: 
  prev => NULL
  next => element #2
  data => "one"

element #2: 
  prev => element #1
  next => NULL
  data => "two"

If we call g_list_foreach() on this list 

  g_list_foreach (list, some_func, "blah");

Where some_func is:

void some_func (gpointer data, gpointer user_data)
{
[...]
}

some_func will be called with "one"/"blah",
and "two"/"blah". But, _never_ with NULL. Since
there are no elements in the list with NULL as
data. Try it.

[ I'll admit, I haven't tested the above code samples,
  but if this was broken, tons and tons of code in
  GTK+ would be broken ]

Regards,
                                        Owen



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