Been a very long day, and rather than post a ton of code, I think I've traced down where the error might be, but I'm unsure what's causing it though. Here is the error:
project043(5827) malloc: *** error for object 0x1001001b0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Suspected class causing issue:
Header triangle.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#ifndef TRIANGLE_H
#define TRIANGLE_H
#include "polygon.h"
#include "point.h"
class Triangle : public Polygon {
public:
Triangle(const Point& p0, const Point& p1, const Point& p2);
~Triangle();
virtual int numberOfPoints() const { return 3; }
virtual Point point(const int& index) const;
private:
Point* m_points;
}; // class Triangle
#endif
| |
Source triangle.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <cassert>
Triangle::Triangle(const Point& p0, const Point& p1, const Point& p2) {
m_points = new Point[3];
m_points[0] = p0;
m_points[1] = p1;
m_points[2] = p2;
}
Triangle::~Triangle() {
delete m_points;
}
Point Triangle::point(const int &index) const {
assert(index>=0);
assert(index<3);
return m_points[index];
}
| |
Parent header polygon.h:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#ifndef POLYGON_H
#define POLYGON_H
#include "point.h"
class Polygon {
public:
virtual int numberOfPoints() const = 0;
virtual Point point(const int& index) const = 0;
virtual double circumference() const;
}; // class Polygon
#endif
| |
Parent source polygon.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include "polygon.h"
#include <cassert>
double Polygon::circumference() const {
assert(numberOfPoints()>2);
double circumference = 0;
for(int i=1; i<numberOfPoints(); ++i)
circumference += point(i-1).distanceTo(point(i));
assert(circumference>0.0);
return circumference;
}
| |
Point class header point.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#ifndef POINT_H
#define POINT_H
class Point {
public:
Point(const double& x=0.0, const double& y=0.0);
double distanceTo(const Point& point) const;
double distanceToOrigin() const;
double x() const {return m_x;}
double y() const {return m_y;}
private:
double m_x;
double m_y;
}; // class Point
#endif
| |
Point class source point.cpp:
1 2 3 4 5 6 7 8 9 10 11 12
|
#include "point.h"
#include <cmath>
Point::Point(const double& x, const double& y) : m_x(x), m_y(y) {}
double
Point::distanceTo(const Point &point) const {
const double distance =
std::sqrt((m_x-point.m_x)*(m_x-point.m_x)+(m_y-point.m_y)*(m_y-point.m_y));
return distance;
}
| |
Points of interest in the main function:
Where vector is declared:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
...
srand(static_cast<unsigned int>(time(NULL)));
std::vector<Polygon*> polygons;
std::vector<Triangle> triangles;
// preallocate the vector
polygons.reserve(SIZE);
triangles.reserve(SIZE);
int triangle_count = 0;
int square_count = 0;
int rectangle_count = 0;
...
| |
Where vector is filled:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
...
for(unsigned i=0; i<SIZE; ++i){
int type = rand()%NUM_OF_TYPES;
switch (type) {
case 0:
{
const Point p0(static_cast<double>(rand()),
static_cast<double>(rand()));
const Point p1(static_cast<double>(rand()),
static_cast<double>(rand()));
const Point p2(static_cast<double>(rand()),
static_cast<double>(rand()));
polygons.push_back(new Triangle(p0, p1, p2));
triangles.push_back(Triangle(p0, p1, p2));
++triangle_count;
}
...
| |
Side length calculation:
1 2 3 4
|
// compute triangles only
double triangle_length = 0.0;
for(unsigned i=0; i<triangles.size(); ++i)
triangle_length += triangles[i].circumference();
| |
I know I said not a lot of code, so I tried to not post anything I didn't find relevant. I'm not looking for someone just to fix it for me, rather just help point out what could be wrong so I can understand the mistake.
Thanks!