title: How do I use the list_for_each macro in list.h (from the Linux kernel) properly?
body: <p>I am having a hard time using list.h from the Linux kernel to provide linked list functionality for my code. I suspect that I almost have functional code, but I am mixing up pointers somewhere.</p>
  <p>How do I use the list_for_each macro properly? In my code, it gets stuck in an infinite loop and does not exit the list. Below is the snippet from my code in which the problem lies (look in the add_kv function):</p>
  <pre><code>dict_entry *alloc_dict(void)
  {
      //allocates the linked list head node
      dict_entry *d = malloc(sizeof(dict_entry));
      INIT_LIST_HEAD(&amp;d-&gt;list);
      return d;
  }
   
  void free_dict(dict_entry *d)
  {
      //TODO: free each dict_entry struct and their keys and values.
      free(d);
  }
  
  int add_kv(dict_entry *d, char *key, char *value)
  {
      if(!key || !d) return 0; //if key or d is null, return 0
  
      struct list_head *p; //serves as the cursor
      dict_entry *entry; //empty dict_entry
      entry = alloc_dict(); //allocate memory for it
  
      list_for_each(p, &amp;d-&gt;list){
          d = list_entry(p, dict_entry, list); //CHANGED TO d FROM entry
          printf(&quot;gothere, p = %p\n&quot;,p); // something in here is creating an infinite loop. p is moving back and forth. this is the big problem in this code
          if(strcmp(entry-&gt;key, key) == 0){
              free(entry-&gt;value);
              entry-&gt;value = 0;
              entry-&gt;value = malloc(strlen(value));
              strcpy(entry-&gt;value, value);
              return 1; //how do i get rid of entry?
          }
      }
      //If you haven't returned by now, continue on to add a new entry at the end of the list 
      entry-&gt;key = malloc(strlen(key)); //allocate memory for the key
      strcpy(entry-&gt;key, key); //copy the key value to the key in the entry
      entry-&gt;value = malloc(strlen(value)); //allocate memory for value
      strcpy(entry-&gt;value, value); //copy value value to the value in the entry
   
      list_add(&amp;entry-&gt;list,&amp;d-&gt;list); //tacks the list of the new entry onto the existing list (provided as d)
      return 1;
  }
  </code></pre>
  <p>Below is the list_for_each macro from list.h, for reference:</p>
  <pre><code>/**
   * list_for_each    -   iterate over a list
   * @pos:    the &amp;struct list_head to use as a loop cursor.
   * @head:   the head for your list.
   */
  #define list_for_each(pos, head) \
      for (pos = (head)-&gt;next; pos != (head); pos = pos-&gt;next)
  </code></pre>
  <p>And here is the list_entry macro from list.h, also for reference:</p>
  <pre><code>/**
   * list_entry - get the struct for this entry
   * @ptr:    the &amp;struct list_head pointer.
   * @type:   the type of the struct this is embedded in.
   * @member: the name of the list_struct within the struct.
   */
  #define list_entry(ptr, type, member) \
      container_of(ptr, type, member)
  </code></pre>
  <p>...and the dict_entry struct I'm using:</p>
  <pre><code>  6 typedef struct {
    7   char *key;
    8   char *value;
    9   struct list_head list;
   10 }dict_entry;
  </code></pre>
  <p>...and when run, this happens:</p>
  
  <pre class="lang-none prettyprint-override"><code>gothere, p = 0x1178050
  gothere, p = 0x1178020
  gothere, p = 0x1178050
  gothere, p = 0x1178020
  </code></pre>
  <p>over and over again.</p>
  <p>A good explanation on how to implement lists with list.h can be found <a href="https://web.archive.org/web/20150310021226/www.makelinux.net/ldd3/chp-11-sect-5" rel="nofollow noreferrer">here</a> for reference.</p>