0

Im trying to create a matrix with different dimensions and it works fine for a 5 by 3 or a 3 by 3 but if i try making a 2 by 3 matrix then I get a bad excess code. In other words if i try to make my x smaller than my y in my (x,y) pair then I get bad excess.

In my header file I have:

#ifndef fasdf_dynn_h
#define fasdf_dynn_h

#include <iostream>
#include <fstream>
#include<string>
#include <cstdlib>
#include <vector>

using namespace std;
template <class T>

class MatrixdynVector{

public:
    MatrixdynVector();
    MatrixdynVector(int m,int n);
    MatrixdynVector(T diagonal, int n, int m);

    template <class H>
    friend ostream& operator <<(ostream& outs, const MatrixdynVector<H> &obj);

private:
    int m,n;
    int** matrix;
};


#endif

and in my cpp file I have:

#include "dynn.h"

template <class T>
MatrixdynVector<T>::MatrixdynVector(){

    //creates a 3 by 3 matrix with elements equal to 0
    m=3;
    n=3;

    matrix=new int*[m];

    for(int i=0;i<m;i++)
        matrix[i]=new int[n];

    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
            matrix[i][j]=0;


}

template <class T>
MatrixdynVector<T>::MatrixdynVector(int x,int y)
{
    //creates a matrix with dimensions m and n with elements equal to 0

    m=x;
    n=y;

    matrix=new int*[m];

    for(int i=0;i<m;i++)
        matrix[i]=new int[n];

    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
            matrix[i][j]=0;
}


template <class T>
ostream& operator <<(ostream& outs, const MatrixdynVector<T> & obj)
{
    for (int i = 0; i < obj.m; i++){
        for (int j = 0; j < obj.n; j++)
            outs << " "<< obj.matrix[i][j];
        outs<<endl;
    }
    outs<<endl;
    return outs;
}

int main()
{
    MatrixdynVector<int> B;//works fine
    MatrixdynVector<int> A(2,3);//bad excess 


    cout<<B;
    cout<<A;
}
2
  • 2
    Excess? Access? Where is your destructor? And why don´t you use vectors?
    – deviantfan
    Jul 4, 2015 at 0:31
  • @deviantfan I already did this with vectors but I need to do this with dynamic array and the destructor is not the issue at the moment
    – Brogrammer
    Jul 4, 2015 at 0:39

1 Answer 1

1

When you create the matrix in the two constructors, the internal loop initialized j but then it increments i. I think that is the problem.

I would suggest also a different way to allocate the matrix:

int *data = new int[m * n];
int **matrix = new int*[m];
for (int r = 0; r < m; ++r)
    matrix[r] = &(data[r * n]);

I assumed you have m rows and n columns. You do less allocations and to clean it up you just delete matrix and data pointers.

6
  • I fixed the internal loop part but whats wrong with the way I allocated the matrix? Also fixing the loop still gives me bad excess code
    – Brogrammer
    Jul 4, 2015 at 0:38
  • About the allocations, your way is working perfectly fine, it's only less efficient: every allocation has a cost, moreover you allocate each rows separately meaning that the matrix will not be contiguous in memory. If you need to scan all the matrix many times, it's better having it contiguous in memory.
    – Arrigo
    Jul 4, 2015 at 0:47
  • You can assign a int to a int * in the block matrix[r] = data[r * n]; invalid conversion from ‘int’ to ‘int*’
    – Rodolfo
    Jul 4, 2015 at 0:48
  • @guploo please either delete the third constructor from the header file or implement it (you can leave it empty)
    – Arrigo
    Jul 4, 2015 at 0:54
  • @Arrigo I just figured out my error though I dont know why it does that but I messed up in my constructor with my for loop. Also the third function is irrelevent but i think rod already knows that
    – Brogrammer
    Jul 4, 2015 at 0:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.