Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Row swap a float array in C++

I have a matrix stored in a float array, and I need to swap a row of one matrix into another.

This is what I have done so far. The matrices m and m_new are a matrix class, where, ‘float* m = new float(N*N)’ is the class variable ‘m’. I made a pointer to the row of the matrix where it needs to be swapped, and I used memcpy for the size of a row, ‘N’, to place that section of ‘m’ into ‘m_new’. However, the values of m_new do not change, and I am confused why.

for (int variable : partition) {
    float* m_pt = m->m + (variable * N * sizeof(float));
    float* m_new_pt = m_new->m + (row * N * sizeof(float));
    memcpy(&m_new_pt, &m_pt, N*(sizeof(float)));
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To fix this issue, you should remove the & operator in the memcpy function.

for (int variable : partition) {
    float* m_pt = m->m + (variable * N);
    float* m_new_pt = m_new->m + (row * N);
    memcpy(m_new_pt, m_pt, N * sizeof(float));
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading