Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Hello, I am still having problem with leaks.
- //Caused by createNew method and passing reference argument
- #include <cstdlib>
- #include <cstdlib>
- #include <cmath>
- #include <vector>
- #include <iostream>
- #include <fstream>
- #include <cstring>
- #include <sstream>
- #include <map>
- using namespace std;
- class GeneralMatrix {
- protected:
- int width;
- int height;
- public:
- //Stores values of matrix size
- GeneralMatrix(int nr, int nc) {
- height = nr;
- width = nc;
- }
- virtual ~GeneralMatrix(){
- }
- GeneralMatrix & add(const GeneralMatrix & m2) {
- //Check for compatible matrixes
- if (height != m2.height || width != m2.width) {
- cout << "Matrix sizes must match!" << endl;
- return *this;
- }
- //Create new empty matrix
- GeneralMatrix &m3 = createNew(height, width);
- //Set every number to sum of this and given matrix
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < m2.width; j++) {
- double val = m2.get(i, j);
- if (val != 0) {
- val += get(i, j);
- m3.set(i, j, val);
- }
- }
- }
- return m3;
- }
- };
- class RegularMatrix : public GeneralMatrix {
- protected:
- vector<double>data;
- public:
- //Constructor
- RegularMatrix(int nr, int nc, const vector<double>& nums) : GeneralMatrix(nr, nc) {
- data = nums;
- }
- GeneralMatrix & createNew(int row, int col) {
- int n = row*col;
- //Prepare vector of zeros
- vector<double>zeros;
- for (int i = 0; i < n; i++) {
- zeros.push_back(0);
- }
- //Call constructor
- GeneralMatrix *ret = new RegularMatrix(row, col, zeros);
- return *ret;
- }
- };
- int main(int argc, char** argv) {
- double mm3[] = {10, 2, 3, 0, 0, 8, 0, 4, 2, 2, 6, 0, 0, 0, 0, 5};
- vector<double>k;
- for (int i = 0; i < 16; i++) {
- k.push_back(mm3[i]);
- }
- GeneralMatrix *d = new RegularMatrix(4, 4, k);
- (*d).add(*d);
- delete d;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement