Simple C++ Dynamicly allocated Matrix with only one memory allocation I was bothered with existing matrix allocation methods that take variable rows and columns, so I made a small class that only needs one memory allocation.

template &lt;typename T&gt;
class Matrix{
    T* data;
    size_t rows, columns;

public:
    Matrix(const size_t rows, const size_t columns) : rows(rows), columns(columns) {
        // One single allocation to store data
        data = new T[rows+columns + 1];
    }

    T* operator[](const size_t r) {
        return data+(r*columns);
    }
};

// Example

int main() {

    Matrix&lt;int&gt; mat(5,3);

    mat[2][1] = 1;

    //...

}
Since you have the new keyword, you should make a destructor and delete[].

~Matrix()
{
    delete[] data;
    data = nullptr;
}

Note that it is not necessary to set data = nullptr in the destructor, because it will be deleted as soon as the destructor is called.

You are missing a copy constructor which can cause problems when assigning one matrix to another

What happens if the number passed in [] when slicing from the matrix is out of bounds?

Have a glance at std::initialzer_list in C++, it will simplify assigning values to the matrix.

You will have to overload the arithmetic operators ( +, -, *, /) if you want to perform any kind of operations between two matrices

Lastly, I was a little confused to see size_t rather than std::size_t, if you have using std::size_t in your source code, you should have it here too to avoid confusion.