Re: GLib/GList on Windows XP

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

 



-----BEGIN PGP SIGNED MESSAGE-----
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.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (MingW32)
Comment: Using GnuPG with Mozilla - http://www.enigmail.net/

iQEcBAEBAgAGBQJQQPB7AAoJEOs4Jb6SI2CwIlwH/izsqKi3iCoQ8nW7UIX0hg/r
ix1+nAUVTA15YlwJ8iCLeLTKtK0RAd/UxYc/iMOmOfLSk5/bF1oj9FZnog2ytbXI
U7uLjWQM+mvdqpjhafgBg4zjv7Fw46rWRJ30aBl598cUMaOxUzJImQHpAvWvWpry
QcBBiZGzJKMKp6megiAiiMiOpSyxLbdKnsv994DYnw4WbOTDfaF3XphRM/FiLWGl
kr+cbxnOLe8l6RpbHOkvVU/+LQ+RqCyma2SckqOMs84yYkqJqDrk1Zp4Gmecl8xZ
piaalkNEIh7d2CC/sA4Yh8+OJ/XfP3wMizGoF/W8c3KMNnGT2/V8NCbTvDeUEOo=
=1bX7
-----END PGP SIGNATURE-----
_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
https://mail.gnome.org/mailman/listinfo/gtk-list


[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux