Re: why does it look like linked list traversing misses the head element?

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

 



On Sun, 23 Mar 2008, Manish Katiyar wrote:

> On Sun, Mar 23, 2008 at 12:46 PM, Robert P. J. Day
> <rpjday@xxxxxxxxxxxxxx> wrote:
> >
> >   i'm sure i'm misreading something, but when i look at the macro
> >  definition of "__list_for_each" in list.h:
> >
> >  #define __list_for_each(pos, head) \
> >         for (pos = (head)->next; pos != (head); pos = pos->next)
> >
> >  i could swear that this traversal will visit each node in the list
> >  except for the initial head element.
> >
> >   look closely:  given a starting address of "head", the
> >  initialization starts things off at (head)->next, and continues
> >  traversing as long as pos != head.  so wouldn't this traversal end up
> >  *not* visiting the list element addressed by "head" itself?  or is
> >  that what it's supposed to do?
>
> Yes, it will miss. And it is supposed to because your head is just
> kind of *sentinel* element and doesnt hold any *useful* data other
> than next and prev pointers.

i had assumed as much, but that seems to fly in the face of what i've
read about these kernel linked lists -- that you can traverse them by
starting at any arbitrary element in the list and just following the
pointers.  if you do that, and that "sentinel" element is part of the
list, won't you eventually reach it and try to process it normally
even though it contains no useful data? or is there something else
weird going on here that i don't get?

maybe i better take a closer look at those macros.

rday
--

========================================================================
Robert P. J. Day
Linux Consulting, Training and Annoying Kernel Pedantry:
    Have classroom, will lecture.

http://crashcourse.ca                          Waterloo, Ontario, CANADA
========================================================================

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux