title: Seg fault when trying to access two dimensional vector element
body: <p>I'm trying to model a set-associative cache with C++11, but I'm going wrong somewhere with my initialization or my method of accessing, or perhaps I need to come up with a better data structure to use...</p>
  
  <p>Here are my private variable declarations:</p>
  
  <pre><code>private:
      int numSets; // Use this to control indexing
      std::vector&lt;std::vector&lt;unsigned long long&gt; &gt;cache;
  </code></pre>
  
  <p>Here's my constructor (played around with resize/reserve and initializing the values a bunch):</p>
  
  <pre><code>        SetAssociativeCache(int associativity) : numSets(512/associativity){
  
          // Initialize the cache
          for(int i = 0; i &lt; numSets; i++){
              for(int j = 0; j &lt; (512/numSets); j++)
                  cache[i][j] = 0;
          }
  </code></pre>
  
  <p>And here's where I'm accessing it inside a member function:</p>
  
  <pre><code>unsigned long long line = cache[setIndex][addrOffset]; // Is this right?
  </code></pre>
  
  <p>At the time of this first access, setIndex is set to 0, and addrOffset is set to 20. Upon execution of this line, the program seg faults and crashes.</p>
  
  <p>Can anyone point me in the right direction here? I'm assuming its just a silly mistake I'm not seeing.</p>
  
  <h2>New Code (Still seg faulting)</h2>
  
  <pre><code>    SetAssociativeCache(int associativity) : numSets(512/associativity) {
  
          // Initialize the cache
          for(int i = 0; i &lt; numSets; i++){
              std::vector&lt;unsigned long long&gt; temp;
              cache.push_back(temp);
              for(int j = 0; j &lt; (512/numSets); j++)
                  cache[i].push_back(0);
          }
  </code></pre>
  
  <h2>GDB Output:</h2>
  
  <pre><code>151   unsigned long long line = cache[setIndex][addrOffset];
  (gdb) s
  std::vector&lt;std::vector&lt;unsigned long long, std::allocator&lt;unsigned long long&gt; &gt;, std::allocator&lt;std::vector&lt;unsigned long long, std::allocator&lt;unsigned long long&gt; &gt; &gt; &gt;::operator[] (this=0x28fec4, __n=0)
  at c:/mingw/lib/gcc/mingw32/4.8.1/include/c++/bits/stl_vector.h:771
  771           { return *(this-&gt;_M_impl._M_start + __n); }
  (gdb) s
  std::vector&lt;unsigned long long, std::allocator&lt;unsigned long long&gt; &gt;::operator[] (this=0x100, __n=20)
      at c:/mingw/lib/gcc/mingw32/4.8.1/include/c++/bits/stl_vector.h:771
  771           { return *(this-&gt;_M_impl._M_start + __n); }
  (gdb)
  
  Program received signal SIGSEGV, Segmentation fault.
  0x00404d38 in std::vector&lt;unsigned long long, std::allocator&lt;unsigned long long&gt; &gt;::operator[] (this=0x100, __n=20)
       at c:/mingw/lib/gcc/mingw32/4.8.1/include/c++/bits/stl_vector.h:771
  771           { return *(this-&gt;_M_impl._M_start + __n); }
  (gdb)
  
  Program received signal SIGSEGV, Segmentation fault.
  0x00404d38 in std::vector&lt;unsigned long long, std::allocator&lt;unsigned long long&gt; &gt;::operator[] (this=0x100, __n=20)
      at c:/mingw/lib/gcc/mingw32/4.8.1/include/c++/bits/stl_vector.h:771
  771           { return *(this-&gt;_M_impl._M_start + __n); }
  (gdb)
  [Inferior 1 (process 6096) exited with code 030000000005]
  </code></pre>