Hello!! what's wrong with my dynamic allocation?

Hello!

My homework was to create an class which contain an array of points, and another class that clculate the distance between all the points...

In another words... to create an polygon and clculte its scoope.

The program crashing when I try to delete the old pointer.

this is my Polygon class:
Polygon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  
void Polygon::addPoint(int x, int y)
{
	polyCount++; 
	Vector * help = new Vector[polyCount];
	for (int i = 0;i < polyCount - 1;i++)//coping the content
	{
		help[i].x = this->polyArray[i].x;
		help[i].y = this->polyArray[i].y;
	}
	delete[]polyArray;
	polyArray = help;//coping pointer


	//entering new point
	polyArray[polyCount].x = x;
	polyArray[polyCount].y = y;
}

int Polygon::getX(int index)
{
	return polyArray[index].x;
}

int Polygon::getY(int index)
{
	return polyArray[index].y;
}

Polygon::Polygon() //constructor
{
	polyCount = 0;
	//this->polyArray = new Vector[size];
	polyArray = new Vector;
}

Polygon::Polygon(int size) //constructor
{
	polyCount = 0;
	//this->polyArray = new Vector[size];
	polyArray = new Vector;
}

Polygon::Polygon(const Polygon & temp) //copy constructor
{
	polyCount = temp.polyCount;
	polyArray = new Vector[polyCount];
	for (int i = 0;i < polyCount;i++)
	{
		polyArray[i].x = temp.polyArray[i].x;
		polyArray[i].y = temp.polyArray[i].y;
	}
	//*polyArray = *(temp.polyArray);
	
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct Vector
{
	int x, y;
};
class Polygon
{
private:
	int polyCount;
	Point *polyArray; // = new Vector;
public:
	void addPoint(int , int); //adding new point, like "setX, setY"
	int getX(int); //getting value of x by index.
	int getY(int); //getting value of y by index.

	Polygon();
	Polygon(int sides); // constractor
	~Polygon() {}; //destractor
	Polygon(const Polygon & poly); //copy constructor
	void polygonScope(); //calculate the scope of the polygon

	friend class Point;

};

Thanks!!
Last edited on
AddPoint() assumes that the polyArray is an array, but in the constructor you allocate just one item. Try changing line 34 to polyArray = new Vector[1];
Also, shouldn't the constructor at line 37 use the sides parameter? Otherwise what is the purpose of that constructor?
Changed... still not work...
This an overloading constructors... the truth that I don't really remember why I did it...
Maybe I've thougth about making some default.... Anyway is there another idea?

Thanks!!
Your
1
2
	Point *polyArray; // = new Vector;
	friend class Point;

creates a questions:
What is a Point?
Why does a pointer to Point point to Vector objects?
Why does it have an unlimited access to Polygon?

Dynamic memory management in a class requires that you obey the Rule of Three (or is it Five in C++11?) Destructor, copy constructor and copy assignment.


How about:
1
2
3
4
5
6
class Polygon
{
private:
  std::vector<Vector> polyArray;
  // other code
};

Let the std::vector manage the memory for you.
(Yesterday I answered here but it is not clear why no update...)

Anyway,
1. One of the requirements of this task is to use two diffrent classes, in diffrent files. So to calculate the distance between two points it uses the other class called Point.
This is the Distance.cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Distance.h"
#include "Polygon.h"

void Point::addTempPoint(int x, int y)
{
	this->x = x;
	this->y = y;
}


float Point::distance(Point a)
{
	return sqrt(pow((x - a.x), 2) + pow((y - a.y), 2));
}


2. I guess I got confused... changed...

3. What does it means?... Does it have any effect about my problem?


About the example, how can I add points without dinamic memory management? And about the three rules.... do they not exist for me?


Thanks!!
Last edited on
3. What does it means?... Does it have any effect about my problem?

Yo do declare that the Point is a friend of Polygon. Why do you do so?
(It does not relate to this problem, but if you do things that you don't understand at all, then you do have a an additional problem.)

About the example, how can I add points without dinamic memory management?
1
2
3
4
5
int main() {
  std::vector<int> foo;
  foo.push_back( 42 );
  return 0;
}

Q: Do we do dynamic memory management in the program above?
A: No. No new or delete anywhere.

Q: Where does the number go then?
A: To dynamically allocated memory.

Q: Wait! I said we don't?
A: We don't, but the std::vector does, in its constructor, push_back() and destructor.

the three rules.... do they not exist for me?

The rule (or guideline) does apply to everyone.
https://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29

However, the rule says what has to be taken care of and if you do use std::vector, you do take care of the problem, because you use components that take care of what the rule says.
Topic archived. No new replies allowed.