0

I made two custom classes: a Vector class(algebraic one, not std::vector), and a Matrix class. I easily implemented Vector class as a array of doubles, i.e i store entries like this:

double *tab = nullptr;
//in constructors
//tab = new double[size]{};

I have a problem with Matrix class though. It is supposed to be a array of pointers to Vectors, i.e:

Vector** tab = nullptr;
//dimensions
int m, n;

What if i simply do not implement it this way, and just do a array of Vectors rather than pointers?

Vector* tab = nullptr 
//...
//destructor looks like this  
Matrix::~Matrix(){
    delete[] tab;
    m = n = 0;
}

The latter seems to be easier in implementation, there is no need to call destructor for every Vector in tab. Am I missing some kind of memory leak? If not why would i ever use the first implementation.

3
  • Try using std:array, or std::vector if the size has to be dynamic. Commented May 9, 2022 at 0:18
  • It does not need to be dynamic, i want it to be constant size. I know i could use built-in std objects, but the point is I want to practice, and i am just having trouble understanding why should i prefer array of pointers to pointers of an object, instead of simply array of pointers to object. Commented May 9, 2022 at 8:35
  • And std::array is exactly that: This container is an aggregate type with the same semantics as a struct holding a C-style array T[N] as its only non-static data member. Except it removed the bothersome decaying to pointer and adds bounds checking in the at method, iterators and a bunch of other useful stuff. Get rid of the pointers completely. Commented May 9, 2022 at 8:56

1 Answer 1

0

If not why would i ever use the first implementation.

Probably the primary reason that you might want to use an array of arrays is that it allows you to to turn the row-column notation of a matrix, i.e. m[1][0] into access to the correct offset without having to do any math. If you matrix is a single dimensional array and someone asks for a given row and column you have to compute the 1D offset from the 2D information you've been given.

Of course there are ways around this. For instance, you could declare your matrix like this:

class Matrix {
public:
  double * _values;
  double ** _rows;

  Matrix() {
    _values = new double[16];
    _rows = new double*[4];
    auto start = _values;
    for (int i = 0; i < 4; ++i) {
      _rows[i] = start;
      start += 4;
    }
  }

  ~Matrix() {
    delete[] _rows;
    delete[] _values;
  }
}

Now, you've allocated one block of doubles and one block of double* for your row offsets. If you want to get a given row r and column c you can access it as _rows[r][c] without doing any computation.

Alternatively, you could also construct your Matrix type out of a collection of Vectors rather than the internal value type directly.

Sign up to request clarification or add additional context in comments.

1 Comment

The thing is, class Vector is implemented as an array of doubles. I also overloaded [] operator. The same goes for Matrix class, it is an dynamic array of my Vector class objects, with [] overloaded, and For example if A is a 5x3 Matrix -> Matrix A(3,5) then it does behave like 2d array (i can access elements this way A[0][2] etc.) I just do not see why i would want to do array of pointers to pointers of an object, instead of simply array of pointers to object

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.