| re: memcpy problem
"ronny" <anyone@anywhere.com> wrote in message
news:gMMPd.1654$ma4.940@newsfe2-gui.ntli.net...[color=blue]
> Can anyone tell me why the following code works fine using an array.[/color]
[...][color=blue]
> double xVal[40000]; // array[/color]
[...][color=blue]
> //Copy values from xVal array to mxArray
>
> memcpy((void *)mxGetPr(X), (void *)xVal, sizeof(xVal));[/color]
Here, sizeof(xVal) will give you the size of the array
(40000*sizeof(double)).
[color=blue]
> but when I try and do the same thing using a pointer like this the data is
> not copied using memcpy.[/color]
[...][color=blue]
> double *xVal = new double[imgSize];[/color]
[...][color=blue]
> memcpy((void *)mxGetPr(X), (void *)xVal, sizeof(xVal));[/color]
Here, sizeof(xVal) will yield the size of the pointer itself and not the
memory to which it points.
[color=blue]
> Can I get memcpy to work using a pointer and if so how? Thank you[/color]
Use imgSize * sizeof(double) instead of sizeof(xVal).
memcpy(mxGetPr(X), xVal, imgSize * sizeof(double));
By the way, the casts to void* aren't needed.
--
David Hilsee |