title: Handling core dump condition
body: <p>I have some simple code (This is a much smaller example of the bigger code I have):</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;unordered_map&gt;
  #include &lt;list&gt;
  
  // List of blocks
  std::list&lt;struct Block&gt; Blocks;
  
  class ObjInfo
  {
  private:
    int size;
  public:
    ObjInfo(int size){this-&gt;size = size;}
    int get_size() {return size;}
  };
  
  struct Block
  {
    // name of the block
    int x;
    // Actual variable stack
    std::unordered_map &lt;int, ObjInfo*&gt; objinfostack;
  };
  
  int main()
  {
  int x = 2;
  while( x &gt;= 0 )
  {
    Block b;
    b.x = x;
    b.objinfostack.insert(std::make_pair(x, new ObjInfo(4)));
    --x;
    Blocks.push_front(b);
  }
  for(std::list&lt;struct Block&gt;::iterator i = Blocks.begin(); i!=Blocks.end(); ++i)
  {
    std::cout &lt;&lt; "here: " &lt;&lt; i-&gt;objinfostack[1]-&gt;get_size() &lt;&lt; '\n';
  }
  
  return 0;
  
  }
  </code></pre>
  
  <p><strong>Quick explanation on what is going on in the code:</strong></p>
  
  <p>Notice the following line leads to the <code>seg fault</code>:</p>
  
  <pre><code>std::cout &lt;&lt; "here: " &lt;&lt; i-&gt;objinfostack[1]-&gt;get_size() &lt;&lt; '\n';
  </code></pre>
  
  <p>It happens because of the invalid access:</p>
  
  <pre><code>i-&gt;objinfostack[1]
  </code></pre>
  
  <p>As I'll get something like this in the <code>Blocks</code> list:</p>
  
  <pre><code>i-&gt;objinfostack[2], i-&gt;objinfostack[1]
  </code></pre>
  
  <p>My question is how can I handle such invalid conditions.</p>