title: C++ Illegal Action when Iterating over Vector Buffer
body: <p>So I am writing this program to search a binary file for a certain chunk, and I get this error that I don't understand.</p>
  
  <p>Here is the Code to the function, that generates the Error<br>
  (also note that the return value is an int, becuase i wanted it to return the size as bytes first, to see if it works)</p>
  
  <pre><code>int GetChunkBuffer(ifstream &amp;imageFile, char chunkName[4])
  {
      //contains the newly opened image as a whole
      vector&lt;unsigned char&gt; imageBuffer(istreambuf_iterator&lt;char&gt;(imageFile), {});
  
      //pointer "array" type of thing
      vector&lt;unsigned char&gt;::iterator bufferIterator = imageBuffer.begin();
  
      for(; bufferIterator != imageBuffer.end(); bufferIterator++)
      {
          if(*bufferIterator == chunkName[0])
          {
              //i think this makes the program faster
              if(*(bufferIterator + 1) == chunkName[1] &amp;&amp;
                 *(bufferIterator + 2) == chunkName[2] &amp;&amp;
                 *(bufferIterator + 3) == chunkName[3])
              {
                  //chunk name matches
                  //get length of chunk
                  unsigned char len[4] = { *(bufferIterator - 4),
                                           *(bufferIterator - 3),
                                           *(bufferIterator - 2),
                                           *(bufferIterator - 1)
                                          };
  
                  unsigned int chunkLength = 0;
  
                  for(int i = 0; i &lt;= 4; i++)
                  {
                      chunkLength += len[i] &lt;&lt; 1;
                  }
  
                  return chunkLength;
              }
          }
      }
  }
  </code></pre>
  
  <p>The File I am opening is a PNG, not sure if that makes any difference.
  Thank you for your help in advance.</p>