Answer the question
In order to leave comments, you need to log in
What is the error (memory handling, destructors)?
I set a class - a point in an N-dimensional space, member functions - a constructor, destructor and setter. I also set the function for calculating the distance between two points Dist. In main I declare 2 points and display the distance. The program works correctly, but after displaying the correct result, it freezes. If there is no destructor, everything is ok. The problem was solved by replacing the formal parameters of Dist with links, i.e.: double Dist(Point& org, Point& dest) instead of double Dist(Point org, Point dest). But still, it is not entirely clear why the first option is not suitable.
#include <iostream>
#include <math.h>
using namespace std;
class Point
{
public:
int _dim;
double* _coords;
Point(int N = 3);
~Point();
void Set();
};
Point::Point(int N):
_dim(N)
{
_coords = new double[N];
for(int i = 0; i < N; i++)
_coords[i] = 0;
}
Point::~Point()
{
delete _coords;
_coords = 0;
}
void Point::Set()
{
cout << "Enter coordinates: ";
for(int i = 0; i < _dim; ++i)
cin >> _coords[i];
}
double Dist(Point org, Point dest)
{
double sumOfSq = 0;
for(int i = 0; i < org._dim; i++)
sumOfSq += pow((dest._coords[i] - org._coords[i]), 2);
return sqrt(sumOfSq);
}
int main()
{
Point A, B;
B.Set();
cout << Dist(A, B);
return 0;
}
Answer the question
In order to leave comments, you need to log in
When passing an object by value, it is copied, but the dynamically allocated memory is not copied, but only the value of the _coords pointer. As a result, you have two objects that refer to the same array. When Dist is finished, the copy of the object is destroyed, the destructor is called, and the memory is freed. And at the end of the program, the destructors of objects A and B are called in which an attempt is made to free memory for _coords a second time, and this is undefined behavior (undefined behavior) in other words, anything can happen at all.
You need to implement the copy constructor yourself or disable it. When you simply delete the destructor, a memory leak occurs, when you pass objects by reference, objects are not copied, so everything is fine. If an object dynamically allocates memory, such as with new or delete, then implementation of the copy constructor is required. You can also disable it by moving the copy constructor to a private section, or for c++11 declare Point(const Point&) = delete; In this case, passing such an object by value will result in a compilation error. This is an important topic, study it.
Anything allocated with new [] is only deleted with delete[]
You have C++, i.e. You can use a vector to store elements without the need for explicit memory allocation:
#include <vector>
// double* _coords;
std::vector<double> _coords;
// _coords = new double[N];
_coords.resize(N);
delete[] _coords;
In this form, copies of objects are created in Dist using the default copy constructor, i.e. just a byte copy. After the completion of Dist, the memory under coords in the copies is freed. When main() completes, the same memory will be freed again, resulting in an error.
Therefore, pass links to Dist!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question