title: Why is preallocation useful for list?
body: <p>I understand it is useful to preallocate a vector or a matrix, because they are always stored in a contiguous memory block.</p>
  
  <p>However, in terms of a list, it can contains elements of difference length and modes. So my first guess is that <strong>a list might merely contains pointers to the true address of its elements</strong>. Am I correct? A related question here <a href="https://stackoverflow.com/questions/16129068/what-is-the-internal-implementation-of-lists">What is the internal implementation of lists?</a> It says the list is essentially an array, but it does't cover how the elements are stored in a list while size of each element may change.</p>
  
  <p>Example 1: If a list contains <code>a,b,c,d,e</code>, and when <code>myList$a&lt;-1:1000000</code>, is the list modified in place (which means only <code>a</code> is updated) or the whole list are copied and updated? </p>
  
  <p>Example 2</p>
  
  <pre><code>&gt; system.time( { myList &lt;- list()
  +                myList$a &lt;- 1:10000000
  +                myList$b &lt;- 1:10000100
  +                myList$c &lt;- 1:10000200 
  +                myList$d &lt;- 1:10000300})
     user  system elapsed 
     0.01    0.02    0.03
  
  &gt; system.time({ myList2&lt;-list(a=1:10000000,b=1:10000100,c=1:10000200,d=1:10000300) })
     user  system elapsed 
     0.00    0.03    0.03 
  </code></pre>
  
  <p>would <code>myList</code> be very inefficient compared to <code>myList2</code> due to no preallocation? Or no noticeable performance difference at all no matter how large <code>a,b,c,d</code> are because the first one copies only pointers a few times more?</p>
  
  <p>Here comes to preallocation. How does it look like for a list? Does it only preallocate the memory for the pointers? If that's the case, I don't see any use since there won't be much data copying for pointers anyway.</p>
  
  <pre><code>&gt; system.time( { myList &lt;- vector("list", 4)
  +                myList[[1]] &lt;- 1:10000000
  +                myList[[2]] &lt;- 1:10000100
  +                myList[[3]]  &lt;- 1:10000200 
  +                myList[[4]] &lt;- 1:10000300
  +                names(myList) &lt;- letters[1:4]})
     user  system elapsed 
     0.01    0.02    0.03 
  </code></pre>