title: Preserving Structs without Dynamic Allocation
body: <p>I've been battling with this problem for a few hours.</p>
  
  <p>I have a doubly linked list implementation that does not require dynamic memory allocation.</p>
  
  <p>Here's the insertion method. It inserts <code>elem</code> right before <code>before</code>. The <code>struct list</code> has two members, <code>struct list_elem *next</code> and <code>struct list_elem *prev</code>. <code>list_elem</code> is a struct that holds some data. Please note that this list implementation is correct. The problem is with the way I am using it. Please read on.</p>
  
  <pre><code>void list_insert(struct list_elem *before, struct list_elem *elem) {
      /* "before" has to be an interior node or tail node to be able to insert "before" it */
      assert (is_interior(before) || is_tail(before));
      assert (elem != NULL);
      elem-&gt;prev = before-&gt;prev;
      elem-&gt;next = before;
      before-&gt;prev-&gt;next = elem;
      before-&gt; = elem;
  }
  </code></pre>
  
  <p>The way this list is initialized is as follows:</p>
  
  <pre><code>void list_init(struct list *list) {
      assert(list != NULL);
      list-&gt;head.prev = NULL;
      list-&gt;head.next = &amp;list-&gt;tail;
      list-&gt;tail.prev = &amp;list-&gt;head;
      list-&gt;tail.next = NULL;
  }
  </code></pre>
  
  <p>My main class does the following:</p>
  
  <pre><code>struct list some_list;
  static struct list_elem head;
  some_list.head = head;
  static struct list_elem tail;
  some_list.tail = tail.
  list_init(&amp;some_list);
  </code></pre>
  
  <p>This basically creates two static <code>list_elem</code> <code>structs</code>, <code>head</code> and <code>tail</code>. And passes it into the initialization function which will wire them up together.</p>
  
  <p>Now to create an element, I do the following - <strong>here's what I am doing wrong</strong></p>
  
  <pre><code>struct list_elem element_struct;
  /* Initialize element_struct members here */
  struct list_elem *data = &amp;element_struct;
  list_insert( list_begin(some_list), data);
  </code></pre>
  
  <p>Now this works to insert one item. Since <code>some_list.head</code> will point to <code>data</code> which will point to <code>some_list.tail</code>. My problem is that I do this in a loop. So, as any list does, it will enter multiple data in there. I either get a <code>segfault</code> or some other error saying my element is not an interior or tail node (due to <code>assert</code>) since in each loop iteration, the data struct, <code>element_struct</code> will be reinitialized. (<code>list_elem</code> holds pointers to each other).</p>
  
  <p>So my question is, how would I preserve the nodes that have been inserted? The list is not supposed to use any dynamic allocation itself. In that case, would I dynamically allocate my <code>list_elem</code> structs, and pass a dynamically allocated element into the list?</p>
  
  <p>If so, how can I dynamically allocate a struct?</p>