I'd like to create a matrix class using the vector library. I have seen many examples of creating a matrix class using standard C arrays, and in each of these, arrays are declared in the private section of the class declaration, and initialized in the constructor function, as such:

template <typename Type>
class matrix
{
      private:
              
              Type * M;
            
              // matrix size
              int nrow;
              int ncol;
              
      public:
             matrix(int m, int n, int val= 0);
            
};

template <typename Type>
matrix<Type>::matrix(int m, int n)
{
             M = new double[m*n];
             nrow = m;
             ncol = n;
}

So here the array is kept in the free store.

My class, instead, uses vectors. As of now, it looks something like:

template <typename Type>
class matrix
{
      private:
              
              // create a single vector to hold the entire matrix:
              // a two-dimensional vector makes it difficult to work with
              // whole columns.
              std::vector<Type> M;
              
             
              // matrix size
              int nrow;
              int ncol;
              
      public:
             matrix(int m, int n, int val= 0);

             Type &operator()(int i, int j);
             Type operator()(int i, int j) const;;
             
};

template <typename Type>
matrix<Type>::matrix(int m, int n, int val)
{
             // default matrix is 1x1, filled with 0.
             M.resize(m*n,val);
             nrow = m;
             ncol = n;
}

But I feel that this is abusing memory quite a bit. Is it advisable to use the free store to house the vector? I have tried doing something like:

template <typename Type>
class matrix
{
      private:
              
              // create a single vector to hold the entire matrix:
              // a two-dimensional vector makes it difficult to work with
              // whole columns.
              std::vector<Type> M;
              
             
              // matrix size
              int nrow;
              int ncol;
              
      public:
             matrix(int m, int n, int val= 0);

             Type &operator()(int i, int j);
             Type operator()(int i, int j) const;;
             
};

template <typename Type>
matrix<Type>::matrix(int m, int n, int val)
{
             M = new std::vector<Type>();
             // default matrix is 1x1, filled with 0.
             M.resize(m*n,val);
             nrow = m;
             ncol = n;
}

But this naive approach doesn't work. My feeling is that it might not be necessary, or even possible, to use the new operator with vectors, but I know not near enough C++ to make a sound conclusion.

>> But I feel that this is abusing memory quite a bit
how so ?

>> My feeling is that it might not be necessary, or even possible, to use the new operator with vectors.
It is possible to use the new operator for vectors. It is not necessary
to use the new operator. Only use it when variable on stack won't
work.

I think your first approach is fine.

>> But I feel that this is abusing memory quite a bit
how so ?

>> My feeling is that it might not be necessary, or even possible, to use the new operator with vectors.
It is possible to use the new operator for vectors. It is not necessary
to use the new operator. Only use it when variable on stack won't
work.

I think your first approach is fine.

Alright, cool beans. I just don't have a great understanding of memory management in C++. I feared that I would be consuming a bunch of memory by instantiating new matrices.

Like in finding the determinant of an nxn matrix, I would create (n!)/2 new minor matrices. Is the memory relinquished when they go out of scope?

>> Is the memory relinquished when they go out of scope?
Yes long as its declared without the new operator, and if it was then
using delete will release the memory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.