Memory storage for transferring a matrix distortion stack, C ++

I need to implement a matrix transpose procedure in C ++. The problem is the signature, the function should be called as follows:

transpose(in_mat[0][0], n, m, out_mat[0][0])

where n and m are sizes. All values ​​are duplications, both matrices and sizes.

Since the code is generated automatically, I cannot fix it.

My workaround is like this:

void transpose(double& in_mat, const double _n, const double _m, double& out_mat)
{
    int n = _n, m = _m;
    double* in_pointer= &in_mat;
    double* out_pointer= &out_mat;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            *(out_pointer+(j*n+i)) = *(in_pointer+(i*m + j));
        }
    }
}

It works great. I built a test case with two matrices of different widths and heights. One is filled with random numbers, the other is filled with zeros. Then the transpose procedure is called and two matrices are compared. Functionality is true.

But it distorts the stack. When starting up in Visual Studio 2015, a warning appears

Runtime check error # 2 - the stack around the 'in_mat' variable is damaged.

? ? .

EDIT:

:

#include <random>
#include <iostream>

void transpose(double& in_mat, const double _n, const double _m, double& out_mat)
{
    int n = _n, m = _m;
    double* in_pointer = &in_mat;
    double* out_pointer = &out_mat;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            *(out_pointer+(j*n+i)) = *(in_pointer+(i*m + j));
        }
    }
}


int main()
{

    double in_mat[5][4];
    double out_mat[4][5];// assign matrix

    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 5; j++) {
            in_mat[i][j] = std::rand();
            out_mat[j][i] = 0;
        }
    }

    double n = 5;
    double m = 4;

    transpose(in_mat[0][0], n, m, out_mat[0][0]);

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (in_mat[i][j] - out_mat[j][i]>0.0001) {
                std::cout << "code is broken" << std::endl; //never reached
            }
        }
    }
    std::cout << "finished" << std::endl;
}
+4
1

( ) , .

double in_mat[5][4];
double out_mat[4][5];// assign matrix

for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 5; j++) {
        in_mat[i][j] = std::rand();
        out_mat[j][i] = 0;
    }
}

j==4 out_mat

+7

Source: https://habr.com/ru/post/1616872/


All Articles