Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
74 views
in Technique[技术] by (71.8m points)

Debugging a c++ function

So we got this code and our task is to debug it. There are 2 Mistakes and one bug in it, i already found the first mistake using the gdb debugger.

1.Mistake: for (std::size_t i = 0; i < size; ++i) this has to be i=1 because you cant divide by zero

2.Mistake: Using the clang++-6.0 compiler with -fsanitize=adress -fsanitize=undefined i get a warning that:

destructor called on non-final 'Polygon' that has virtual functions but non-virtual destructor [-Wdelete-non-virtual-dtor]

Bug: I have no idea where the bug is

Question: Can you help me find the second mistake and the bug that lies in this code?

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <iostream>
#include <memory>
#include <random>
#include <string>
#include <vector>
#include <math.h>

class Shape {
 public:
  virtual std::string name() const = 0;

  virtual int dimension() const { return 0; }

  virtual double volume() const { return 0.0; }
};

class Point : public Shape {
 public:
  virtual std::string name() const { return "point"; }

  virtual int dimension() const { return 2; }

  virtual double volume() const { return 0.0; }

  Point(double x, double y) : _x(x), _y(y) {}

  Point() : _x(0.), _y(0.) {}

  double x() const { return _x; }

  double y() const { return _y; }

 private:
  double _x;
  double _y;
};

class Polygon : public Shape {
 public:
  virtual std::string name() const { return "polygon"; }

  virtual int dimension() const { return 2; }

  virtual double volume() const {
    auto size = _corners.size();
    double result = 0.0;
    for (std::size_t i = 0; i < size; ++i)
      result += _corners[i].x() * _corners[(i + 1) % size].y() -
                _corners[(i + 1) % size].x() * _corners[i].y();

    return 0.5 * result;
  }

  Polygon(const std::vector<Point>& corners) : _corners(corners) {}

 private:
  std::vector<Point> _corners;
};

class PolygonGenerator {
 public:
  PolygonGenerator() : _corner_dist(3, 11), _radius_dist(8, 4) {}

  std::vector<Polygon*> makePolygons(std::size_t count) {
    std::vector<Polygon*> result;
    for (std::size_t k = 0; k < count; ++k) {
      std::vector<Point> corners(_corner_dist(_random));
      for (std::size_t i = 0; i <= corners.size(); ++i) {
        auto radius = _radius_dist(_random);
        auto angle = count / i * 2.0 * M_PI;
        corners[i] = {std::cos(angle) * radius, std::sin(angle) * radius};
      }
      auto polygon = std::make_shared<Polygon>(corners);
      result.push_back(polygon.get());
    }
    return result;
  }

 private:
  // random numnber generator
  std::mt19937_64 _random;
  // distribution for number of corners: [3,11)
  std::uniform_int_distribution<> _corner_dist;
  // distribution for radius from origin of each corner
  std::lognormal_distribution<> _radius_dist;
};

int main(int argc, char** argv) {
  PolygonGenerator gen;

  std::vector<Polygon*> polygons = gen.makePolygons(10);

  for (auto& p : polygons) std::cout << p->volume() << std::endl;
}
question from:https://stackoverflow.com/questions/65942104/debugging-a-c-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...