title: Access Violation Reading Location
body: <p>I have a problem with VC++, simply, I hate it haha. My code seems to be running all fine on my Mac but when I try to run it in VC++, I get this error in debug:</p>
  
  <blockquote>
    <p>Windows has triggered a breakpoint in Assignment1-FINAL.exe.</p>
    
    <p>This may be due to a corruption of the heap, which indicates a bug in
    Assignment1-FINAL.exe or any of the DLLs it has loaded.</p>
    
    <p>This may also be due to the user pressing F12 while
    Assignment1-FINAL.exe has focus.</p>
  </blockquote>
  
  <p>I know for a fact I haven't pressed F12 so I am not sure why I am getting this... Then, when I try to run it in Release mode, I get this:</p>
  
  <blockquote>
    <p>Unhandled exception at 0x00401473 in Assignment1-FINAL.exe:
    0xC0000005: Access violation reading location 0x00347015.</p>
  </blockquote>
  
  <p>This is the code I am using:</p>
  
  <pre><code>int countPointsAboveThreshold(point * points, double threshold_distance) {
      int i = 1;
      int count = 0;
  
      while (points[i - 1].end != true) {
          point pointOne = points[i -1];
          point pointTwo = points[i];
          double distance = distanceBetweenTwoPoints(pointOne, pointTwo);
  
          if (pointTwo.end == true) {
              if (distance &gt; threshold_distance) {
                  count++;
                  return count;
              } else {
                  return count;
              }
          } else if (distance &gt; threshold_distance) {
              count++;
          }
          i++;
      }
      return count;
  }
  
  int totalPoints(point * points) {
      int i = 0;
      while (points[i].end != true) {
          i++;
      }
      return i + 1;
  }
  
  point * findLongPaths(point * points, double threshold_distance) {
      int i = 1;
      int locationToStore = 0;
      int pointsAboveThreshold = countPointsAboveThreshold(points, threshold_distance);
  
      point * pointsByThreshold = new point[pointsAboveThreshold];
      pointValues * pointsToCalculate = new pointValues[pointsAboveThreshold];
  
      while (points[i - 1].end != true &amp;&amp; i &lt; pointsAboveThreshold) {
          point pointOne = points[i - 1];
          point pointTwo = points[i];
  
          //Check to see if the distance is greater than the threshold, if it is store in an array of pointValues
          double distance = distanceBetweenTwoPoints(pointOne, pointTwo);
          if (distance &gt; threshold_distance) {
              pointsToCalculate[i - 1].originalLocation = i - 1;
              pointsToCalculate[i - 1].distance = distance;
              pointsToCalculate[i - 1].final = pointTwo;
              pointsToCalculate[i - 1].stored = false;
  
              //If the final point has been calculated, break the loop
              if (pointTwo.end == true) {
                  pointsToCalculate[i].end = true;
                  break;
              } else {
                  pointsToCalculate[i - 1].end = false;
                  i++;
                  continue;
              }
          }
      }
  
      if (points[0].end == true &amp;&amp; pointsAboveThreshold == 0) {
          point emptyPoint;
          emptyPoint.x = 0.0;
          emptyPoint.y = 0.0;
          emptyPoint.end = true;
  
          pointsByThreshold[0] = emptyPoint;
          return pointsByThreshold;
      }
  
      //Find the point with the lowest distance
      int j = 2;
      //EDITED
      pointValues pointWithLowest;
      pointWithLowest = pointsToCalculate[0];
      while (pointsToCalculate[j - 1].end != true) {
          for (int k = 1; pointsToCalculate[k - 1].end != true; k++) {
              if (pointsToCalculate[k - 1].stored == true) {
                  k++;
                  continue;
              } else {
                  if (pointsToCalculate[k - 1].distance &gt; pointWithLowest.distance) {
                      pointWithLowest = pointsToCalculate[k - 1];
                      k++;
                      continue;
                  } else if (pointsToCalculate[k - 1].distance == pointWithLowest.distance) {
                      if (pointWithLowest.originalLocation &lt; pointsToCalculate[k - 1].originalLocation) {
                          pointWithLowest = pointsToCalculate[k - 1];
                          k++;
                          continue;
                      } else {
                          k++;
                          continue;
                      }
                  } else {
                      pointWithLowest.stored = true;
                      pointsByThreshold[locationToStore] = pointWithLowest.final;
                      locationToStore++;
                      break;
                  }
              }
          }
          //DEBUGGER STOPS HERE
          j++;
      }
      delete[] pointsToCalculate;
      return pointsByThreshold;
  }
  </code></pre>
  
  <p>And this is the main function:</p>
  
  <pre><code>    point *longest_calculated = findLongPaths(p, 1.1);
  std::cout &lt;&lt; "Should equal " &lt;&lt; longest[1].y &lt;&lt; ": " &lt;&lt; longest_calculated[1].y;
      delete longest_calculated;
      cin.get();
      return 0;
  </code></pre>