Re: GLib/GList on Windows XP

Hash: SHA1

On 31.08.2012 20:56, Gary Dumer wrote:
> Hi,
> I'm having a problem that may be the result of my inexperience with
> GLib or it could be an issue with Glib on Windows XP.
> I'm having several problems. The first is with g_list_index always
> returning 0. Am I correct that g_list_index searches the entire
> list starting at 0 to find the requested "data" OR does
> g_list_index start at the current position to find the data? If
> it's the former then I'm having a problem, if it's the latter then
> I assume that I have to set the starting position myself before
> initiating g_list_index.
g_list_index takes head of the list as its first argument. That is,
you must keep a separate GList * pointing to the head (or you need to
use a GList * pointing to a non-head element, and travel the list to
the head).
You can write your own version of g_list_index(), that travels the
list both ways from the given list element, rather than requiring it
to be the list head.

> The second issue is with g_list_nth_prev. The docs suggest that if
> I try to move beyond the beginning of the list, g_list_nth_prev
> will return NULL and the list will remain at the position prior to
> the invalid g_list_nth_prev. For example, if I have a list
> containing 10 elements and I'm positioned at the last element, if I
> do the following:
> if (NULL == (x = g_list_nth_prev(x, 99))) print an error
> message... else proceed...
> the program crashes. But if I do this:
> if (NULL == g_list_nth_prev(x, 99)) print an error message... else 
> x = g_list_nth_prev(x, 9);
> it works correctly. The question is... should it work in the first
> case? My understanding of the docs suggest it should.
> The third issue relates to the second one. Any GList function that
> can potentially advance the list beyond its boundaries is open to
> the same issue above. So any of the following functions can end
> with the same potential results:
> g_list_nth g_list_nth_datag_list_next g_list_previous
> Maybe others as well.

Your problems arise from you not understanding the fact that GList is
not an object, that holds the beginning and the end of the list.
GList is a structure with a pointer to previous GList *element, a
pointer to next GList *element, and a data pointer.
If you need to know where head is, you have to keep a pointer around
(most list-modifying functions will return the new head, so it's easy
to keep it relevant). Or traverse the list from any element upward,
until you hit the head.
Some functions will return a pointer to a particular item in the list,
rather than its head, so you shouldn't overwrite list head with that.
Functions you've mentioned are among them.
And GList does not store any state, so you can't say that "List
remains in a position it was in prior to blahblahblah". The only state
is the head pointer you keep around.
NULL head pointer means "empty" list.
Version: GnuPG v1.4.11 (MingW32)
Comment: Using GnuPG with Mozilla -


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