memcpy problem 
July 23rd, 2005, 01:50 AM
| | | |
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 | 
July 23rd, 2005, 01:50 AM
| | | | 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 | 
July 23rd, 2005, 01:51 AM
| | | | re: memcpy problem
thanks for your reply. It has worked perfect. I guess I still have a lot to
learn!
"David Hilsee" <davidhilseenews@yahoo.com> wrote in message
news:AM6dnV_JMozbApLfRVn-rw@comcast.com...[color=blue]
> "ronny" <anyone@anywhere.com> wrote in message
> news:gMMPd.1654$ma4.940@newsfe2-gui.ntli.net...[color=green]
>> Can anyone tell me why the following code works fine using an array.[/color]
> [...][color=green]
>> double xVal[40000]; // array[/color]
> [...][color=green]
>> //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=green]
>> but when I try and do the same thing using a pointer like this the data
>> is
>> not copied using memcpy.[/color]
> [...][color=green]
>> double *xVal = new double[imgSize];[/color]
> [...][color=green]
>> 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=green]
>> 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
>
>[/color] |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,689 network members.
|