Exit operator overload

Hi there, I am recently writing my own matrix class and I am also overloading the usual operators for matrices. I want to include an if statement inside the addition of two matrices, so that if the number of rows/columns of the matrices differs, the program stops and gives an error message. Thing is I am working with dynamic memory (use of "new statement"), and every object of matrix class is allocated in some memory when created. My question is, how can I break the sum but ensuring that the allocated memory for all matrices are going to be erased? Here is the code, thanks in advance!

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 #include <iostream>
#include <fstream>
#include <cmath>
#include <stdlib.h>
#include "matrix.h"
#include "time.h"

using namespace std;
//OPERATORS OVERLOAD FOR MATRICES
//ADITION
matrix operator+(matrix& M1,matrix& M2){
	int n1,m1,n2,m2;
	double p;//value to fill the matrix
	
	n1=M1.nrows();
	m1=M1.ncolumns();
	n2=M2.nrows();
	m2=M2.ncolumns();
	
	matrix result(n1,m1);
	
	for(int i=0;i<n1;i++){
		for(int j=0;j<m1;j++){
			
			p=M1.get(i,j)+M2.get(i,j);
			result.fill(p,i,j);
			
		}
	}
		
	return result;
		
}
//SUBSTRACTION
matrix operator-(matrix& M1,matrix& M2){
	int n1,m1,n2,m2;
	double p;//value to fill the matrix
	
	n1=M1.nrows();
	m1=M1.ncolumns();
	n2=M2.nrows();
	m2=M2.ncolumns();
	
	if(n1!=n2)||(n2!=m2){
		
		cout<<"matrices must be of the same size. Program terminated "<<endl;
			
		
	}
	
	matrix result(n1,m1);
	
	for(int i=0;i<n1;i++){
		for(int j=0;j<m1;j++){
			
			p=M1.get(i,j)-M2.get(i,j);
			result.fill(p,i,j);
			
		}
	}
		
	return result;
		
}


int main(){
	
	srand(time(NULL));	
	matrix A(2,2);
	matrix B(3,2),C;
   
    A.double_rand(0,1);
    
    B.double_rand(0,1);
    A.printm();
    cout<<endl<<endl;
    B.printm();
    
     C=A+B;
     
     C.printm();
   	// clean matrices objects;	
  
	A.clean();
	B.clean();
	C.clean();
			
	
	return 0;
}
One way is to delete the memory in the destructor, so even if you throw an exception in your add method the memory gets deleted. However you have to catch all exceptions in main. Another benefit of an destructor is that you don't have to remember to call clean on your objects.


So does that mean that if I create the destructor ~matrix() in the header file, then regardless of exceptions or if statements, the memory gets always deallocated after running the code? It is just because, if I want the program to stop because of an error (like adding two matrices with different dimensions), does it return the "0" value of the main function, or it just finishes it with a different return, in which case maybe the destructor is never called? Thank you for the reply!
when I did a big linear algebra project we created our own memory manager. It basically allocated a very large pool of memory with 'new' up front and cut chunks out of that rather than create and destroy memory for every matrix created (there were a ton of intermediates computed for some of the operations). Deleted the big pool at the end of the program. You have to manage what is in use and all that. We did it 'page style', allocating same sized blocks regardless of the size needed, so the memory pool would not fragment. If you do it that way you really just need a vector of bool for allocated/available for each block in the big pool.

This was a fair bit of trouble but the avoiding all the new/delete pairs was a major performance boost. We knew ahead of time exactly what we were doing though, the # of matrices needed and their sizes were known at compile time.

You can also make a "dumb" smart pointer template class that does nothing but new up a pointer and return it, and delete it upon dtor. Its not much more overhead than doing it by hand, avoids missing a delete and leaking memory. the downside is there are specific things you cannot do, such as copy the pointer in the object out to a local, let the object destory itself, and then use the now deleted pointer, because it lacks reference counting and management overhead stuff. Or you can use the c++ smart pointers, but those have their own concerns.



Last edited on
Deleting the memory in the destructor is normally the best option if you have dynamic memory.
However there is no guarantee that the destructor is called. Some cases where the destructor might not b called if the program terminates with abort(), terminate()...
Have a look at stackoverflow.com about this topic.
Of course you could use a vector or smart pointer but then the question arises if their destructors get called. At the end of your app the OS will release all the memory anyway so I think you worry to much.

A simple example with vector instead of pointer:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=13849&lngWId=3
Topic archived. No new replies allowed.