Samuele Pipitone
Samuele Pipitone

Reputation: 97

Building a submatrix from a matrix

I'm trying to split a matrix given into 4, but I'm getting some errors and I can't figure out why. I'm kind of new to the language. Does anybody knows what am I doing wrong?

void split_tl(T **matrice, unsigned int dim){
    if(dim == 1){
        return;
    }

    T **tl = new T*[dim/4];
    for(unsigned int i = 0; i<dim/4;++i){
        tl[i] = new T[dim/4];
    }

    for(unsigned int i=0; i<dim;++i){
        for(unsigned int j=0; j<dim;j++){
            if((i<dim/2) && (j<dim/2)){
                tl[i][j] = matrice[i][j];
            } else{
                std::cout << "no ";
            }
        }
        std::cout << std::endl;
    }
}

In this function I'm trying to obtain the top left corner of the matrix.

int **matrice = new int*[2];

for(unsigned int i = 0; i<2;++i){
    matrice[i] = new int[2];
}

for(unsigned int i = 0; i<2;++i){
    for(unsigned int j = 0; j<2;++j){
        matrice[i][j] = i+j;
    }
}

This is the matrix I'm sending. It is a 2x2 matrix, just for testing purposes.

These are the errors from Valgrind:

==133== Invalid read of size 8
==133== Invalid write of size 4
==133== Process terminating with default action of signal 11 (SIGSEGV)
==133== Access not within mapped region at address 0x0

Upvotes: 0

Views: 311

Answers (2)

What you're doing wrong? You're allocating memory and passing pointers around. Build a proper matrix class, e.g. (very simplified version):

template <typename T, unsigned Rows, unsigned Cols>
struct generic_matrix {
    
    using datatype = T;
    
    static constexpr unsigned rows = Rows;
    static constexpr unsigned cols = Cols;
    
    datatype data[rows][cols];

    constexpr datatype& operator()(unsigned row, unsigned col) noexcept
    { return data[row][col]; }
    
    constexpr const datatype& operator()(unsigned row, unsigned col) const noexcept
    { return data[row][col]; }
    
};

Submatrix:

/* Returns a submatrix of the matrix m, 
 * by deleting the row r and the column c.*/
template <typename M>
auto submatrix(const M& m, unsigned r, unsigned c) noexcept
{
    generic_matrix<typename M::datatype, M::rows-1, M::cols-1> res;
    
    for (unsigned row = 0, i = 0; row < M::rows; ++row) {
        if (row == r) continue; //this row we do not want
        for (unsigned col = 0, j = 0; col < M::cols; ++col) {
            if (col == c) continue; //this col we do not want
            res(i,j) = m(row,col);
            ++j;
        }
        ++i;
    }

    return res;
}

Print:

template <typename M>
void printmatrix(const M& m) noexcept
{
    for (unsigned r=0; r < M::rows; ++r) {
        for (unsigned c=0; c < M::cols; ++c) {
            std::cout << m(r,c) << ' ';
        }
        std::cout << '\n';
    }
}

Test:

int main()
{
    int n=0;
    generic_matrix<int, 3, 3> m;
    for (int r = 0; r < 3; ++r)
        for (int c = 0; c < 3; ++c)
            m(r,c) = ++n;

    printmatrix(m);
    std::cout << '\n';

    printmatrix(submatrix(m, 0, 0));
    std::cout << '\n';

    printmatrix(submatrix(m, 1, 1));
    std::cout << '\n';

    printmatrix(submatrix(m, 2, 2));

    return 0;
}

Note: It is just a hint. As you can see, no allocation nor casting is needed.

Upvotes: 0

Wishwesh Gandhi
Wishwesh Gandhi

Reputation: 58

If dim is the side of a matrix, allocating to a quarter matrix should be dim/2.

Below in the code you are using :

if((i<dim/2) && (j<dim/2)){
  tl[i][j] = matrice[i][j];
}

here tl may exceed the allocation

Upvotes: 1

Related Questions