473,322 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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
Jul 23 '05 #1
2 3012
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: manya | last post by:
Ok, it's been a while since I've done the whole memcpy stuff with C++ and I'm having a hard time remembering everything. I hope, however, that you can help me with my problem. I memcpy a...
33
by: Case | last post by:
#define SIZE 100 #define USE_MEMCPY int main(void) { char a; char b; int n; /* code 'filling' a */
6
by: myhotline | last post by:
hi all im very confused about using memcpy and i have three questions....memcpy takes a pointer to src and a pointer to dest and copies src to destination...but im very confuzed about when to...
11
by: Peter Pichler | last post by:
A colleague encountered an interesting problem. Suppose we have a C function like this: void WRITE_THING(void* addr, THING t) { memcpy(addr, &t, sizeof t); } to copy a THING to any byte...
3
by: ebrahimbandookwala | last post by:
HI everyone I am supposed to pass back a pointer to a struct from a function whos definition cannot be changed flight_t * get_item(field_t field , void * data) the problem I encounter is...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
3
by: naren | last post by:
Iam not getting the correct pros and cons of the strcpy() and memcpy() some where i read for short strings strcpy is faster and for large strings memcpy is faster.. in strcpy() there is a single...
6
by: danu | last post by:
Basically I'm trying to do here is put the uint32_t seq_num into the first 4 bytes of the buf. But every time I do this, it wouldn't get copied in to the buf at all. Can anyone point out the...
18
by: Mark | last post by:
Hi List, I want to write a function to copy some data out of a hardware buffer. The hardware can change the contents of this buffer without it being written to by my function. I want to use...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.