By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 2,075 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

memcpy problem

P: n/a
Can anyone tell me why the following code works fine using an array.

<snip>
double xVal[40000]; // array

mxArray *X = NULL; //MatLab mxArrays

..

..

..

//Create mxArray

X = mxCreateDoubleMatrix(imgSize, 1, mxREAL);

//Copy values from xVal array to mxArray

memcpy((void *)mxGetPr(X), (void *)xVal, sizeof(xVal));
<snip>

but when I try and do the same thing using a pointer like this the data is
not copied using memcpy.

<snip>

double *xVal = new double[imgSize];

..

..

memcpy((void *)mxGetPr(X), (void *)xVal, sizeof(xVal));

<snip>



Can I get memcpy to work using a pointer and if so how? Thank you
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"ronny" <an****@anywhere.com> wrote in message
news:gM****************@newsfe2-gui.ntli.net...
Can anyone tell me why the following code works fine using an array. [...] double xVal[40000]; // array [...] //Copy values from xVal array to mxArray

memcpy((void *)mxGetPr(X), (void *)xVal, sizeof(xVal));
Here, sizeof(xVal) will give you the size of the array
(40000*sizeof(double)).
but when I try and do the same thing using a pointer like this the data is
not copied using memcpy. [...] double *xVal = new double[imgSize]; [...] memcpy((void *)mxGetPr(X), (void *)xVal, sizeof(xVal));
Here, sizeof(xVal) will yield the size of the pointer itself and not the
memory to which it points.
Can I get memcpy to work using a pointer and if so how? Thank you


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
Jul 23 '05 #2

P: n/a
thanks for your reply. It has worked perfect. I guess I still have a lot to
learn!

"David Hilsee" <da*************@yahoo.com> wrote in message
news:AM********************@comcast.com...
"ronny" <an****@anywhere.com> wrote in message
news:gM****************@newsfe2-gui.ntli.net...
Can anyone tell me why the following code works fine using an array.

[...]
double xVal[40000]; // array

[...]
//Copy values from xVal array to mxArray

memcpy((void *)mxGetPr(X), (void *)xVal, sizeof(xVal));


Here, sizeof(xVal) will give you the size of the array
(40000*sizeof(double)).
but when I try and do the same thing using a pointer like this the data
is
not copied using memcpy.

[...]
double *xVal = new double[imgSize];

[...]
memcpy((void *)mxGetPr(X), (void *)xVal, sizeof(xVal));


Here, sizeof(xVal) will yield the size of the pointer itself and not the
memory to which it points.
Can I get memcpy to work using a pointer and if so how? Thank you


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

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.