memcpy problem

Discussion in 'C++' started by ronny, Feb 13, 2005.

  1. ronny

    ronny Guest

    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
    ronny, Feb 13, 2005
    #1
    1. Advertising

  2. ronny

    David Hilsee Guest

    "ronny" <> wrote in message
    news:gMMPd.1654$...
    > 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
    David Hilsee, Feb 13, 2005
    #2
    1. Advertising

  3. ronny

    ronny Guest

    thanks for your reply. It has worked perfect. I guess I still have a lot to
    learn!

    "David Hilsee" <> wrote in message
    news:...
    > "ronny" <> wrote in message
    > news:gMMPd.1654$...
    >> 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
    >
    >
    ronny, Feb 13, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ninan Thomas
    Replies:
    3
    Views:
    405
    Kevin Goodsell
    Aug 22, 2003
  2. Bren
    Replies:
    8
    Views:
    2,008
    Stephen Howe
    Sep 4, 2003
  3. Paul Schouten
    Replies:
    2
    Views:
    2,417
    Vladimir Khvostov
    Jan 3, 2005
  4. Ninan Thomas
    Replies:
    3
    Views:
    1,882
    Kevin Goodsell
    Aug 22, 2003
  5. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    problem with memcpy and pointers/arrays confusion - again

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Mar 9, 2006, in forum: C Programming
    Replies:
    39
    Views:
    1,228
    Ben Pfaff
    Mar 21, 2006
Loading...

Share This Page