C ++ delete link

I'm still learning C ++ and have a question that might be obvious, or maybe I just don't know what I'm trying to do. I have functions that take a matrix (the class I wrote that has a correctly written destructor) and create a new matrix from it, returning a link to the new one. I need iterations, perhaps tens of thousands of times on these matrices, so I need to make sure that I have no memory leaks. So the question is, how to remove the matrix that I no longer need to make space for the next? Here is the code I'm trying to get without leaks:

DynamicMatrix<double> x0 = getX0(n); DynamicMatrix<double>exactU = getExactU(n); DynamicMatrix<double> b = getB(n) * w; DynamicMatrix<double> x1 = getX1(x0, b, w, n); while( !isConverged(exactU,x1,e) ){ delete x0; //<<<<< This doesn't work. Nor does delete &x0. x0 = x1; x1 = getX1(x0, b, w, n); } 

Each of the getX () methods creates a pointer to the matrix and returns a reference to the matrix, as in getX0 ():

 DynamicMatrix<double> &getX0(int n){ DynamicMatrix<double>* mat1 = new DynamicMatrix<double>(n * n,1); for (int i = 1 ; i <= n; i++){ for (int j = 1; j <= n; j++){ mat1->set((i-1)*n +j, 1, 0); } } return *mat1; } 

So, the call is "delete X0" because it needs a pointer. 'delete & X0' says that the freed pointer was not highlighted. What is the right way to do this? Or am I doing something completely wrong? With matrices too large and with too many iterations, my large hard drive runs out of space, which I can only assume that I have a memory leak in abundance.

+4
source share
5 answers

Stroustrup R'lich Fkhtag .

The entry MyType myVar = MyFunction() creates a new object using a constructor that takes the return type myFunction as an argument. Everything that was returned by myFunction is then discarded - in your example, getX0 returns a reference to an object that was dynamically allocated and therefore leaked.

Seriously, though - try creating matrices on the stack (without new ) and return them as they are. It should not cause too many problems, because they seem to dynamically distribute their data inside, and I suspect that NRVO will be used to avoid copying (the return matrix will be directly built in the appropriate place. x0 and Magic x1 below can be implemented as follows way:

 x0.swap(x1); DynamicMatrix<double> temp = getX1(x0, b, w, n); x1.swap(temp); 

Since the swap operation can be implemented on your dynamic matrix in terms of quick pointer replacement (which is very fast) instead of the actual copy of the data, this should be extremely fast.

+6
source

You must use pointers. Statement

 DynamicMatrix<double> x0 = getX0(n); 

Makes a copy of the matrix. Do you want to

 DynamicMatrix<double> *getX0(int n){ DynamicMatrix<double>* mat1 = new DynamicMatrix<double>(n * n,1); ... return mat1; } 

Then

 DynamicMatrix<double> *x0 = getX0(n); ... delete x0; 
+2
source

if getX() returns a pointer, you should write as the first line:

 DynamicMatrix<double>* x0 = getX0(n); 

This will make more sense since you will return a new pointer. Then you should delete it, as shown a few lines below.

Note that you can save a lot of trouble by using boost::shared_ptr :

 typedef boost::shared_ptr<DynamicMatrix<double> > dyn_matrix_ptr; dyn_matrix_ptr x0 (getX0(n)); // use x0 as a normal pointer ... // You don't have to manually delete it, it will be deleted automatically. 
+1
source

Your mistake here:

 DynamicMatrix<double> x0 = getX0(n); 

You do not have to use pointers. You can return the link to the new object. To remove the memory, just take the link address. Turning to the address of the link, you will receive the address of the referent; you should be able to call

 // receive newed memory in a reference DynamicMatrix<double>& x0 = getX0(n); // &x0 should give you the address of the allocated memory. delete &x0; 
0
source

The rules for DynamicMatrix<double> are fundamentally the same as for int .

If it was pushed onto the stack as the "auto" variable, then the correct way to clear it is to do nothing - just release it from the scope. You want to streamline your code as much as possible so that this happens.

If it was assigned a "new", clear it with "delete".

Please, never dynamically highlight something, and then return it by reference. Return the pointer. Actually, do not do this. Use the smart pointer class. You are welcome.

Please do not dynamically allocate things if you do not need it. Just make a local value and return it - by value (this is how you handle the fact that you cannot return a link to a non-static local). You would never, ever, think of writing code like the following, right?

 int& give_me_a_value() { int* result = new int(rand()); return *result; } 

Again: the rules for DynamicMatrix<double> are basically the same as for int . That's why you implement copy constructors, assignment operators, and destructors: so that it really works the way you would reasonably expect.

0
source

All Articles