2011-08-21 57 views
0

我有一个VC++的问题,只是,我讨厌它哈哈。我的代码似乎在我的Mac上运行良好,但是当我尝试在VC++中运行它时,出现调试中的错误:访问冲突阅读地点

Windows在Assignment1-FINAL.exe中触发了一个断点。

这可能是由于堆损坏引起的,这表示 Assignment1-FINAL.exe或其中已加载的任何DLL的错误。

这可能也是由于用户按下了F12而 Assignment1-FINAL.exe有焦点。

我知道,所以我不知道为什么我收到了...那么我还没有按下F12一个事实,当我尝试在Release模式下运行它,我得到这个:

Assignment1-FINAL.exe中的0x00401473未处理的异常: 0xC0000005:访问冲突读取位置0x00347015。

这是我使用的代码:

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 > threshold_distance) { 
       count++; 
       return count; 
      } else { 
       return count; 
      } 
     } else if (distance > 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 && i < 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 > 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 && 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 > pointWithLowest.distance) { 
        pointWithLowest = pointsToCalculate[k - 1]; 
        k++; 
        continue; 
       } else if (pointsToCalculate[k - 1].distance == pointWithLowest.distance) { 
        if (pointWithLowest.originalLocation < 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; 
} 

这是主要的功能:

point *longest_calculated = findLongPaths(p, 1.1); 
std::cout << "Should equal " << longest[1].y << ": " << longest_calculated[1].y; 
    delete longest_calculated; 
    cin.get(); 
    return 0; 
+0

那么在代码中调试器停止了吗? – user885074

+0

我添加了一条评论,它接近它的末尾,它是'j ++;' – Brandon

+0

这里有很多事情要做 - 你可以发布'point'的定义吗? – Chad

回答

0

您发布此为C++,但它似乎可以用很少的东西C++实际上是关于对象的。此代码读取更像C.

只是一些注意事项:

  1. 使用C++,你不需要做typedef struct {...} point,做struct point {...}做什么你正在尝试做的。
  2. 如果您使用stl::vector而不是c-array,那么您的循环将变得更简单,并且您将不需要您的函数totalPoints()。你也可以从point和删除成员变量endpointValues
  3. 你正在堆上创建很多变量,而不是在堆栈上,没有很好的理由。使用stl::vector(或其他标准容器),局部变量和引用可以极大地简化内存管理并避免这些奇怪的崩溃。

我要在你的代码深入了解一下,看看我可以给你一些更具体的指导,但你真的应该做一些进一步阅读到什么C++提供了C.我想看看cplusplus.comC++ FAQ。还有一些优秀的书籍建议here

+3

Offtopic。他没有要求编程风格的教训。 –

+0

是的,没错。我只是想提出一些我认为会让他最接近他的目标的建议。如果问题的作者认为它没有帮助,我会删除答案。 – ColGraff

+0

他正在做功课,而且很多代码中的坏事都是由导师施加的。不幸的是,这很常见。 – john

2

初始想法: 断言在哪里?您将countPointsAboveThreshold()中的Points *作为数组访问,但根本不检查边界,以确保不会传递数组的末尾。这将是我的第一个检查记忆跺脚动作的领域。此外,直接指针调用非常C.嘿,你没有检查任何数组调用的界限。危险...

长度为0的新数组可能安全,也可能不安全。我会小心的。

无论何时,当我在一个声明中看到[i - 1]时,我会感到紧张。很容易阅读垃圾在我== 0

i,j,k循环四元组嵌套ifs混合继续和休息?否。重新思考这个逻辑。这是方式,太复杂了。

您正在使用pointsToCalculate []分配的内存提前返回。内存泄漏。

我可能建议将您的最后一个功能分成多个部分以简化逻辑?

我讨厌K & R型括号。你的选择 - 不是在这里开始圣战:P

除此之外,我会去我的第一个建议,并确保你的结束布尔总是设置,你不会出界。如前所述,stl :: vector和几个引用(最好是const)在这里是你的朋友。

+0

新增一个长度为零的数组非常安全。 – john

+0

我曾想过这件事 - 不过我不会去做任何事情。有点像释放NULL是有效的... –

0

你的这部分代码听起来很奇怪对我说:

if (distance > threshold_distance) { 
     pointsToCalculate[i - 1].originalLocation = i - 1; 
     pointsToCalculate[i - 1].distance = distance; 
     pointsToCalculate[i - 1].final = pointTwo; 
     pointsToCalculate[i - 1].stored = false; 
... 

我认为你需要使用其他指标变量(比我其他 - 1)填充pointsToCalculate!

我将重写这个部分是这样的:

int i = 1; 
int index = 0; 

// if points[i - 1].end is true how you could access points[i] ? 
while (points[i].end != true && i < 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 > threshold_distance) { 
     pointsToCalculate[index].originalLocation = i - 1; 
     pointsToCalculate[index].distance = distance; 
     pointsToCalculate[index].final = pointTwo; 
     pointsToCalculate[index].stored = false; 

     ++ index; 
    } 

    ++i; 
} 

pointsToCalculate[index].end = true; 

**另请注意,你需要在阵列中的至少两个点或再次获得访问冲突,所以你需要检查这一点,你在“countPointsAboveThreshold”函数中也有同样的问题,您需要修复它。

请检查语法和拼写错误;)

但是什么办法,我强烈建议下最后两个职位的建议了。