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 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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: Case |
last post by:
#define SIZE 100
#define USE_MEMCPY
int main(void)
{
char a;
char b;
int n;
/* code 'filling' a */
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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....
|
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
|
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...
|
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...
| |