簡體   English   中英

malloc():內存損壞(快速)c ++

[英]malloc(): memory corruption (fast) c++

我是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 ***

我在網上搜索了相同的錯誤,但是我不知道該怎么辦。

點.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);
}

謝謝。

由於您沒有發布完整的程序,因此應注意以下幾點:

convexHull(Point Points[], int n)

在該函數中,您不會檢查n是否在Points數組的范圍內。 您應該在整個函數中使用vector。 例如:

 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”類風格故意編碼的習慣。 您有矢量,因此請充分利用它。

下期:

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類型。

從錯誤的外觀以及您所說的崩潰的位置(一個聲明)來看,它很可能在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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM