title: Structs on the stack (ANSI C)
body: <p>Having started to study Ulrich Dreppers "What every programmer should know about memory"  [1] series I got stuck when trying to reproduce the examples presented in section</p>
  
  <p>3.3.2 Measurements of Cache Effects</p>
  
  <p>As far as I understand the structures should be allocated on the stack since then they are in memory one after another. Verifying this with a small program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #define NUMPAD 0
  
  struct listelement
  { 
      struct listelement *next;
      long int padding[NUMPAD];
  };
  
  typedef struct listelement elem;
  
  int main()
  {
      int size_elem = sizeof(elem);
      printf("Size of a list elem: %i\n", size_elem);
  
      elem _1, _2, _3;
  
      _1.next = &amp;_2;
      _2.next = &amp;_3;
      _3.next = &amp;_1;
  
      elem *first, *last, *curr;
  
      first = &amp;_1;
      last = &amp;_3;
      curr = &amp;_1;
  
      int k=0;
  
      while(1)
      {
          printf("Element at %p", curr);
          for (int i=0; i&lt;NUMPAD; i++)
          {
              (curr-&gt;padding)[i] = i+k;
              printf("%ld ", (curr-&gt;padding)[i]);
          }
          printf("\n");
          if (curr == last)
              break;
          k++;
          curr = curr-&gt;next;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>When running the program the output is:</p>
  
  <pre><code>Size of a list elem: 8
  Element at 0x7fff5fbff540
  Element at 0x7fff5fbff530
  Element at 0x7fff5fbff520
  </code></pre>
  
  <p>The differences between the memory addresses, however is 16 why not 8? When increasing NUMPAD the difference seems to grow even more, for example for NUMPAD=2 I get a difference of 511.</p>
  
  <p>I did the tests on a macbook pro running OSX 10.6 64bit.</p>
  
  <p>[1] <a href="http://lwn.net/Articles/252125/" rel="nofollow">http://lwn.net/Articles/252125/</a></p>
  
  <p>Update: I also played with incrementing/decrementing the pointers. It seems to work in 32bit mode, but not in 64bit mode. Adding the code</p>
  
  <pre><code>first--;
  printf("first-- %p\n", first);
  if (first == &amp;_2) {
      printf("Decrementing works.\n");
  }
  
  macbook:blah nils$ gcc -m32 -DNUMPAD=0 -g -std=c99 -o blah blah.c &amp;&amp; ./blah
  Size of a list elem: 4
  Element at 0xbffff5b8
  Element at 0xbffff5b4
  Element at 0xbffff5b0
  first-- 0xbffff5b4
  Decrementing works.
  macbook:blah nils$ gcc -DNUMPAD=0 -g -std=c99 -o blah blah.c &amp;&amp; ./blah
  Size of a list elem: 8
  Element at 0x7fff5fbff530
  Element at 0x7fff5fbff520
  Element at 0x7fff5fbff510
  first-- 0x7fff5fbff528
  </code></pre>
  
  <p>I wonder how this makes sense.. maybe I should just place all the structs into an array.</p>