title: C++ basic dynamic allocation
body: <p>In this example I don't understand why the first element of the list cannot be dynamically allocated.
  I understand the solution is BAD because I will be losing the pointer to "a" and it will be dangling. What I don't understand is why in that case my function returns an empty list.</p>
  
  <p>I have simplified the example as requested:</p>
  
  <pre><code>ListNode* createList() {
      //Why not - ListNode* a = new ListNode(0); ?
      ListNode list(0); ListNode * a = &amp;list;
      for(int i=0; i &lt; 5; i++){
          a-&gt;next = new ListNode(0);
          a = a-&gt;next;
      }
      //return a-&gt;next;
      return list.next;
  }
  </code></pre>