0

Possible Duplicate:
memcpy(), what should the value of the size parameter be?

I want to copy a dynamic array:

g  = new complex<double> [N*N]; 

to in of type fftw_complex*:

in = (fftw_complex*) fftw_alloc_complex(N*N);

When I use:

in= (fftw_complex*) fftw_alloc_complex(N*N);
memcpy( in , g , sizeof(g));
out = (fftw_complex*) fftw_alloc_complex(N*N);
my_plan = fftw_plan_dft_2d(N,N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(my_plan);
fftw_destroy_plan(my_plan);
fftw_free(in);
fftw_free(out);

I get weird results. Just zeros for both in and out.

Strangely, when I use instead:

memcpy( &in , &g , sizeof(g));

I get exactly the correct results, but with this the line:

fftw_free(in);

...does not work. It gives "unhandled exception" error.

Community
  • 1
  • 1
  • This doesn't sound right. Please construct a [minimal test-case](http://sscce.org). (Also note that as you've found that `in` is wrong, you may as well eliminate everything after the `memcpy`, as it's irrelevant.) – Oliver Charlesworth Jan 29 '13 at 14:19
  • do you have the source code of fftw_free and fftw_alloc_complex? I think these methods do more than a simple alloc and free, that's what might cause the problem – ppetrov Jan 29 '13 at 14:19
  • Why don't you think of designing your class fftw_complex copy-construcetd from complex? – Manmohan Singh Jan 29 '13 at 14:22
  • 2
    sizeof pointer is always the same, 4 or 8, depending on the platform. perhaps you want to say `N*N*sizeof(complex)`. – Marius Bancila Jan 29 '13 at 14:23
  • @MariusBancila - that's an answer, not a comment. In fact, it is the correct answer. – Robᵩ Jan 29 '13 at 14:24
  • @Robᵩ, Marius: In which case, I'd arguably close this as "too localized" or as a suitable duplicate. We probably don't need the millionth question whose answer is "you're using sizeof incorrectly"! – Oliver Charlesworth Jan 29 '13 at 14:25
  • Marius, I tried what you said and indeed it gave me a different answer but not the correct one :(. I saw here http://stackoverflow.com/questions/9681932/using-memcpy-with-array-name that there is a problem using memcpy with pointers, so I'm trying to figure out how to use it . – Yair Forchevsky Jan 29 '13 at 15:01

0 Answers0