title: Corrupted stack while testing Doom 3 Stack implementation
body: <p>I get </p>
  
  <blockquote>
    <p>Stack around the variable 'a' was corrupted.</p>
  </blockquote>
  
  <p>While trying out this code:</p>
  
  <pre><code>    #include "Stack.h"
      #include &lt;iostream&gt;
  
      struct product {
          int weight;
          float price;
      } ;
  
      void main(void){
          product a = {1, 4.0f};
          product b = {2, 5.0f};
          product c = {3, 6.0f};
  
          idStackTemplate&lt;product, sizeof(product)&gt; stack;
  
          stack.Add(&amp;a);
          stack.Add(&amp;b);
          stack.Add(&amp;c);
  
          product * first, * second, *third;
  
          third = stack.Get();
          second = stack.Get();
          first = stack.Get();
  
          std::cout &lt;&lt; first-&gt;price &lt;&lt; "\t" &lt;&lt; first-&gt;weight &lt;&lt; "\n";
          std::cout &lt;&lt; second-&gt;price &lt;&lt; "\t" &lt;&lt; second-&gt;weight &lt;&lt; "\n";
          std::cout &lt;&lt; third-&gt;price &lt;&lt; "\t" &lt;&lt; third-&gt;weight &lt;&lt; "\n";
      }
  </code></pre>
  
  <p>Stack.h source code is located <a href="https://github.com/id-Software/DOOM-3/blob/master/neo/idlib/containers/Stack.h" rel="nofollow">here</a>!</p>
  
  <p>Otherwise the code prints out the correct values.</p>
  
  <p>I'm a beginner/intermediate in c/c++ so can you please help me understand how idStack works and what am I doing wrong.</p>
  
  <p>I have tried creating stack object with idStack(type, next) macro - from what I understand you should put one of the elements of product for "next" parameter so that it calculates offset. It never worked correctly (with either weight or price) although I didn't get any stack corruption errors but the variables didn't print out correctly.</p>
  
  <p>I have also seen some code in Doom 3 source code that uses dynamic allocation with memory pool which would kinda explain this but I can't see this technique being used here...</p>
  
  <p>EDIT: I feel like I shoud allocate enough memory on heap (3*sizeof(a)) with that custom allocators and then initialize a at the beginning of that newly allocated memory. Am I leaning in the right direction?</p>