title: stl map, set error: memory clobbered past end of allocated block
body: <p>As I tried to solve this interview problem: find the sum of continuous element in an array which equals to a target number, so I come up with the following code. However, I really don't understand why it has some memory allocation problem. Here is the <a href="http://codepad.org/t9WyjTPd" rel="nofollow">link</a> to the full code. when I tried to insert the second element of currSum into the set and map, it will have some error message like "memory clobbered past end of allocated block". Isn't set, map dynamically allocated for insert? I really don't understand why it's not working as I think.</p>
  
  <p>I also paste the full code here:</p>
  
  <pre><code>        #include &lt;map&gt;
          #include &lt;set&gt;
          #include &lt;iostream&gt;
          using namespace std;
  
          void print_array(int arr[], int start, int end)
          {
              for(int i=start;i&lt;=end;i++)
                  cout&lt;&lt;arr[i]&lt;&lt;" ";
              cout&lt;&lt;endl;
          }
  
          //given an array, find the continous sum of the array which is a target number
          void findTargetSum(int arr[], int target, int sizeArr)
          {
              map&lt;int, int&gt; valIdxMap;
              set&lt;int&gt; prevSet;
              int* currSum= new int(sizeArr+1);
              currSum[0]=0;   
              for(int i=1;i&lt;=sizeArr;i++)
              {
                  currSum[i]=currSum[i-1]+arr[i-1];
              }
  
              //now the problem is to find two elements i, j in array currSum,where currSum[j]-currSum[i]=target &amp;&amp; j&gt;i
              for(int i=0; i&lt;=sizeArr;i++)
              {
                  int tmp=currSum[i]-target;
                  set&lt;int&gt;::iterator iter=prevSet.find(tmp);
                  if (iter !=prevSet.end())
                  {
                      cout&lt;&lt;"located one range of array elements with sum to"&lt;&lt;target&lt;&lt;endl;
                      int startIdx=valIdxMap[*iter];
                      print_array(arr,startIdx,i-1);
                  }
                  else
                  {
                      prevSet.insert(currSum[i]);
                      valIdxMap.insert(make_pair(currSum[i],i));
                  }
              }
              delete currSum;
          }
  
          void testfindTargetSum()
          {
              int tst_arr[]={2,4,5,-1,3,8};
              int target=11;
              findTargetSum(tst_arr,11,6);
          }
  
          int main()
          {
             testfindTargetSum();
             return 1;
          }
  </code></pre>