0

I've been using memcpy for a while with one-dimensional arrays but when I try two-dimensional weird things happen. The following program illustrates the issue:

using namespace std;
#include <iostream>  
#include <string.h>
#include <complex>

int main() {

    int n=4;
    complex<double> **mat1=new complex<double>*[n], **mat2=new complex<double>*[n];
    for(int i=0;i<n;i++) {mat1[i]=new complex<double>[n]; mat2[i]=new complex<double>[n];}

    for(int i=0;i<n;i++) {
        for(int j=0;j<n;j++) mat1[i][j]=complex<double>(i*j, i+j);
    }

    cout << endl << "Matrix 1:" << endl;
    for(int i=0;i<n;i++) {
        for(int j=0;j<n;j++) cout << mat1[i][j] << "  ";
        cout << endl;
    }

    cout << endl << "memcpy" << endl << endl;
    memcpy(mat2, mat1, n*n*sizeof(complex<double>));

    cout << "Matrix 1:" << endl;
    for(int i=0;i<n;i++) {
        for(int j=0;j<n;j++) cout << mat1[i][j] << "  ";
        cout << endl;
    }

    cout << endl << "Matrix 2:" << endl;
    for(int i=0;i<n;i++) {
        for(int j=0;j<n;j++) cout << mat2[i][j] << "  ";
        cout << endl;
    }
}

The first printout of mat1 works fine but in the second and that of mat2 the first half of the elements are gibberish. Any idea what's going on?

jorgen
  • 3,425
  • 4
  • 31
  • 53
  • 2
    Don't use memcpy in C++ as it doesn't play nice with classes. Use `vector>> mat1(Y, vector>(X));` for your 2d array. To copy `mat2 = mat1;` – Neil Kirk Aug 13 '14 at 13:24
  • 2
    In C++ you really shouldn't be using `memcpy` or C-style arrays at all. – John Dibling Aug 13 '14 at 13:24
  • 4
    An array of *pointers to* one dimensional arrays is not a two dimensional array. – CB Bailey Aug 13 '14 at 13:24
  • 1
    I doubt that you can expect `memcpy` to work correctly on those `complex` data structures, as they are most likely not consecutive in memory (i.e., they have member variables that are pointing to different memory areas). – barak manos Aug 13 '14 at 13:24
  • possible duplicate of [C / C++ How to copy a multidimensional char array without nested loops?](http://stackoverflow.com/questions/2225850/c-c-how-to-copy-a-multidimensional-char-array-without-nested-loops) – kiranpradeep Apr 23 '15 at 09:27

2 Answers2

2

You are not using the two dimensional arrays. You are using the 1 dimensional arrays and another one which is storing the set of pointers. Giving that there is no guarantee that your arrays will reside in the memory continuously.

Every time you are asking in the code for new allocation your program will find a (new) memory region to fit in the (new) array. If you will print the array storing the pointers you can read where your arrays actually reside.

cerkiewny
  • 2,761
  • 18
  • 36
1

As other stated, you shouldn't use memcpy here.

But for the record, when doing a memcpy this way, your erasing the pointers in mat2, creating a leak, and your copying something way too big in mat2, f***ing up your memory. The correct memcpy way would be to memcpy independently each entry of mat1 in mat2.