memcpy problem

R

ronny

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
 
D

David Hilsee

ronny said:
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.
 
R

ronny

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

David Hilsee said:
ronny said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top