title: How are arrays "implemented" in C?
body: <p>An <strong>array</strong> is notably <em>not</em> a <strong>pointer</strong>. To be sure, both <em>lvalues</em> seem to contain the (1-dimensional) coordinate of some position in (1-dimensional) virtual memory. But consider this example.</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  int main(){
    char buffer0[4096];
    char* buffer1 = malloc(4096);
    printf("lvalue %16p  sizeof %lu\n", (void *) buffer0, sizeof(buffer0));
    printf("lvalue %16p  sizeof %lu\n", (void *) buffer1, sizeof(buffer1));
  // Example output:  lvalue   0x7ffcb70e8620  sizeof 4096
  // Example output:  lvalue         0x7a4420  sizeof 8
  }
  </code></pre>
  
  <p><em>Practical</em> differences that come to mind are:</p>
  
  <ol>
  <li>Arrays know how big (in bytes) they are (and, by extension, they know <em>how many elements</em> they have); pointers don't (but <code>malloc()</code> <em>must</em> know how big a pointer is, to know how much to <code>free()</code> given just the pointer...!)</li>
  <li>Arrays are "garbage collected" (no need to <code>free()</code> them); pointers must be freed manually (if they own a non-trivial amount of memory, ie. through <code>malloc()</code>)</li>
  <li>Arrays "live" in the stack (high virtual memory addresses, at least on my platform); pointers "live" in the heap (low virtual memory addresses)</li>
  <li>Arrays decay to pointers when passed to functions</li>
  <li>Arrays cannot be resized; pointers can</li>
  </ol>
  
  <p>Overall, arrays seem to be much smarter (but less versatile) than pointers (they know how big they are, how many elements they have, and they have automatic memory management).</p>
  
  <hr>
  
  <p><strong>Questions</strong></p>
  
  <ol>
  <li>How do arrays "know" how big they are? How is this implemented?</li>
  <li>In general, how are arrays implemented in the C language? (Does the compiler do this, or does the kernel?</li>
  </ol>