Connecting Tech Pros Worldwide Help | Site Map

memcpy problem

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 12:50 AM
ronny
Guest
 
Posts: n/a
Default memcpy problem

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



  #2  
Old July 23rd, 2005, 12:50 AM
David Hilsee
Guest
 
Posts: n/a
Default 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


  #3  
Old July 23rd, 2005, 12:51 AM
ronny
Guest
 
Posts: n/a
Default 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]


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

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 220,989 network members.