Connecting Tech Pros Worldwide Help | Site Map

memcpy problem

ronny
Guest
 
Posts: n/a
#1: Jul 23 '05
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


David Hilsee
Guest
 
Posts: n/a
#2: Jul 23 '05

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


ronny
Guest
 
Posts: n/a
#3: Jul 23 '05

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]


Closed Thread


Similar C / C++ bytes