Re: Cautions with g_list_next()



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, Sep 12, 2006 at 12:15:13AM -0300, Fabricio Rocha wrote:
Hi pals,

      Are there any known recomendations about GLists and the g_list_next() 
macro? I found no remarks in the official documentation until I got 
problems with constructions like this...

      element = g_list_first (member->names);
      while (element) {
              ...
              element = g_list_next (member->names);
      }

As David already pointed out (but using other words), you'd never
advance in this loop. You'll be getting always the second element of
list (so the loop will work right for one-element lists ;-)

Think about it: before the while(), "element" points to the first element
in the list; after the last line in the while(), "element" will *always*
point to the second element in the list.

I'm assuming here that you don't change "member->names" in the "..."
part you haven't shown us.

A nice idiom would be:

       for(element = g_list_first(member->names);
           element;
           element = element->next) {
           ...
       }

You'll meet this idiom in many places in the GTK+ sources.

HTH
- -- tomÃs
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFFBkcIBcgs9XrR2kYRAmxxAJ9NB7Xi8Sl6kcywRj34rMqoGvKYLQCdE4Mm
3iSAYGys4KU0P5c/azF75Hs=
=+Ivi
-----END PGP SIGNATURE-----




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