malloc():内存损坏(快速)c ++

时间:2014-03-19 16:25:17

标签: c++ memory malloc

我是c ++的新手,我尝试使用C++计算Point(x,y,z)集的凸包。

我在main方法中调用了以下方法:

vector<Point> convexHull(Point Points[], int n) {
    vector<Point> v;
    // Find the bottommost Point
    int ymin = Points[0].getY();
    int min = 0;
    for (int i = 1; i < n; i++) {
        int y = Points[i].getY();

        // Pick the bottom-most or chose the left most Point in case of tie
        if ((y < ymin) || (ymin == y && Points[i].getX() < Points[min].getX()))
            ymin = Points[i].getY(), min = i;
    }
    // Place the bottom-most Point at first position
    Points[min] = Points[0].swap(Points[min]);

    // Sort n-1 Points with respect to the first Point. A Point p1 comes
    // before p2 in sorted ouput if p2 has larger polar angle (in 
    // counterclockwise direction) than p1
    p0 = Points[0];
    qsort(&Points[1], n - 1, sizeof (Point), compare);

    // Create an empty stack and push first three Points to it.
    stack<Point> S; //  on debug the project I find that the problem is here 
    S.push(Points[0]);
    S.push(Points[1]);
    S.push(Points[2]);

    // Process remaining n-3 Points
    for (int i = 3; i < n; i++) {
        // Keep removing top while the angle formed by Points next-to-top, 
        // top, and Points[i] makes a non-left turn
        while (orientation(nextToTop(S), S.top(), Points[i]) != 2)
            S.pop();

        S.push(Points[i]);
    }

    // Now stack has the output Points, print contents of stack
    while (!S.empty()) {
        Point p = S.top();
        cout << "(" << p.getX() << ", " << p.getY() << ", " << p.getZ() << ")" << endl;
        v.push_back(Point(p.getX(), p.getY(), 0));
        S.pop();
    }
    return v;
}

它给出了这个错误:

*** glibc detected *** /home/user/NetBeansProjects/DGILOG-ask/dist/Debug/GNU-Linux-x86/dgilog-task: malloc(): memory corruption (fast): 0x08de1238 ***

我在网上搜索了同样的错误,但我不知道该怎么做。

Point.cpp

#include <iostream>
#include <math.h>
#include <ostream>

using namespace std;

#include "Point.h"

Point::Point() : x(0), y(0), z(0) {
}

Point::Point(ostream &strm) {
    strm << "Type the abscissa: ", cin >> this->x;
    strm << "Type the ordinate: ", cin >> this->y;
    strm << "Type the applicate: ", cin >> this->z;
}

Point::Point(float x, float y, float z) : x(x), y(y), z(z) {
}

/**
 * Destructor
 */
Point::~Point() {
}

//Other methods

float Point::dist2D(Point &other) {
    float xd = x - other.x;
    float yd = y - other.y;
    return sqrt(xd * xd + yd * yd);
}

float Point::dist3D(Point &other) {
    float xd = x - other.x;
    float yd = y - other.y;
    float zd = z - other.z;
    return sqrt(xd * xd + yd * yd + zd * zd);
}

Point Point::swap(Point p) {
    Point aux(x, y, z);
    x = p.x;
    y = p.y;
    z = p.z;
    return aux;
}

void Point::print(ostream &strm) {
    strm << "Point(" << this->x << "," << this->y << "," << this->z << ")" << endl;
}

bool Point::operator<(const Point &p) const {
    return x < p.x || (x == p.x && y < p.y);
}

感谢。

2 个答案:

答案 0 :(得分:5)

由于您没有发布完整的程序,因此以下是您需要注意的一些事项:

convexHull(Point Points[], int n)

在该函数中没有任何地方检查n是否在Points数组的范围内。你应该在整个函数中使用向量。例如:

 int ymin = Points[0].getY();
 int min = 0;
 for (int i = 1; i < n; i++) {
    int y = Points[i].getY();

如果我将NULL指针作为第一个参数(或者甚至是无效指针)传递,或者如果n太大,则会出现访问冲突。使用向量可以大大减少或彻底消除这些问题。使用vector,您可以使用size()成员函数进行完整性测试,以确保Point具有相关的条目数。现在,你的功能无法进行这样的测试。

下一期:

S.push(Points[0]);
S.push(Points[1]);
S.push(Points[2]);

你怎么知道至少有3个条目?你不知道,并且该功能无法检查。你所拥有的只是一个被传递的指针,以及一些任意数字n。如果您正在使用C ++,那么您就不应该习惯于以类似C&C的风格进行刻意编码。你有矢量,所以要利用它。

下一期:

qsort(&Points[1], n - 1, sizeof (Point), compare);

由于您没有发布Point是什么,如果Points是非POD类型,则qsort()的这种用法会导致未定义的行为。

停止使用qsort()。在C ++程序中使用qsort()表明编码器要么是1)使用他们习惯的C程序员(通常遵循令人惊讶的意外重现)或2)新手C ++程序员谁正在阅读C书或程序作为编写适当的C ++程序的指导。

使用std :: sort() - 您正在编写C ++应用程序,而不是C应用程序。 std :: sort是类型安全的,更易于使用和设置,适用于遵循严格弱顺序的POD和非POD类型。

答案 1 :(得分:2)

通过看错误,以及你说它崩溃的地方(一个声明),它很可能在qsort函数中。它是您在代码中唯一的C库调用,并且错误不能在堆栈的声明中,因为它只是一个声明。

你应该做的第一件事是检查界限

vector<Point> convexHull(Point Points[], int n) {
  vector<Point> v;
  if(n <= 3){ 
     // error
  }else{
    //the chunk of code
  } 
  return v;
}

但等等......还有更多,假设你想用一个向量

替换Points数组
std::vector<Point> convexHull(vector<Point>::iterator begin, vector<Point>::iterator end) {
  std::vector<Point> returnVal;
  if(n <= 3){ 

  }else{
    //the chunk of code
  } 
  return returnVal;
}
// just don't forget to check your returnVal for empty size, error handling is a must

或者你可以使用你的c风格的旧学校方法和矢量...我不建议这样做,因为你不再学习迭代器,你应该这样做。

vector<Point> Points(100);
vector<Point> v;
if(convexHull(&Points[0], Points.size())) ///< how would you be testing this
{
   //yay
}

哦,顺便说一下,实现std :: sort非常简单,不要害怕,就像这样

std::sort (&Points[1], &Points[1]+ (n-1));

如果迭代器中的点数

会更容易
std::sort (begin+1, end);

你甚至可以进一步使用std :: advance,你应该

vector<Point>::iterator it = begin;   
std::advance(it,1);
std::sort (it, end);

总之,第一部分如何看待迭代器?

std::vector<Point> convexHull(vector<Point>::iterator begin, vector<Point>::iterator end)
{
  vector<Point> v;
  // Find the bottommost Point
  int ymin = begin->getY();
  vector<Point>::iterator l_it = begin; 
  vector<Point>::iterator min_position = begin;
  std::advance(l_it, 1);
  for (; l_it != end; ++l_it) 
  {
      int y = l_it->getY();

      // Pick the bottom-most or chose the left most Point in case of tie
      if ((y < ymin) || (ymin == y && l_it->getX() < min_position->getX()))
      {
          ymin = l_it->getY();
          min_position = l_it;
      }
   /// MORE CODE
}
相关问题