title: Accessing an array out of bounds gives no error, why?
body: <p>I am assigning values in a C++ program out of the bounds like this:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  int main()
  {
      int array[2];
      array[0] = 1;
      array[1] = 2;
      array[3] = 3;
      array[4] = 4;
      cout &lt;&lt; array[3] &lt;&lt; endl;
      cout &lt;&lt; array[4] &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>The program prints <code>3</code> and <code>4</code>.  It should not be possible.  I am using g++ 4.3.3</p>
  
  <p>Here is compile and run command</p>
  
  <pre><code>$ g++ -W -Wall errorRange.cpp -o errorRange
  $ ./errorRange
  3
  4
  </code></pre>
  
  <p>Only when assigning <code>array[3000]=3000</code> does it give me a segmentation fault.</p>
  
  <p>If gcc doesn't check for array bounds, how can I be sure if my program is correct, as it can lead to some serious issues later?</p>
  
  <p>I replaced the above code with</p>
  
  <pre><code>vector&lt;int&gt; vint(2);
  vint[0] = 0;
  vint[1] = 1;
  vint[2] = 2;
  vint[5] = 5;
  cout &lt;&lt; vint[2] &lt;&lt; endl;
  cout &lt;&lt; vint[5] &lt;&lt; endl;
  </code></pre>
  
  <p>and this one also produces no error. </p>