Thread: Help memcpy!

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    4

    Help memcpy!

    Hi all, this is my first post and i'm a beginner in c programming,
    i need help to understand what i'm doing wrong.

    I'm not getting the expected result:

    Code:
    ...
    size_t size = sizeof(uint32_t);
    ...
    void *ret = malloc(size * width * height);
    if (ret == NULL) return 1;
    ...
    memcpy(ret + (y * width * size) + x, data + (y * width * size) + w, size);
    ...
    i'm getting the expected result through, with this piece of code:

    Code:
    ...
    size_t size = sizeof(uint32_t);
    ...
    void *ret = malloc(size * width * height);
    if (ret == NULL) return 1;
    ...
    ((uint32_t *)ret)[y * width + x] = ((uint32_t *)data)[y * width + w];
    ...
    but as the size is dynamic, i can't use this, and i really need something like the fist case.

    Thanks in advance, and sorry if my english is not the best.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What are the results in first case?
    Last edited by std10093; 07-04-2012 at 03:52 PM.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Kozay Novel View Post
    Code:
    ...
    memcpy(ret + (y * width * size) + x, data + (y * width * size) + w, size);
    ...
    i'm getting the expected result through, with this piece of code:

    Code:
    ...
    ((uint32_t *)ret)[y * width + x] = ((uint32_t *)data)[y * width + w];
    ...
    Your offset is different between these two cases.For example in 1st case you write (y * width * size) + x but if you compare it with the second one,i believe that you wish to write (y * width + x )* size
    Last edited by std10093; 07-04-2012 at 04:22 PM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    My guess is (y * width + x ) * size is closer to [y * width + x].

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > memcpy(ret + (y * width * size) + x, data + (y * width * size) + w, size);
    Well this shouldn't even compile, since you can't do arithmetic on a void pointer.

    It you make ret like this
    unsigned char *ret;

    then the address calculation you have should work with
    (y * width + x ) * size
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    4
    Thanks std10093 and stahta01,
    Sorry but the in the original, i copy wrongly from a commentary in my code (as i try many offsets).

    it should be:

    Code:
    ...
    size_t size = sizeof(uint32_t);
    ...
    void *ret = malloc(size * width * height);
    if (ret == NULL) return 1;
    ...
    memcpy(ret + (y * width) + x, data + (y * width) + w, size);
    ...
    and:

    Code:
    ...
    size_t size = sizeof(uint32_t);
    ...
    void *ret = malloc(size * width * height);
    if (ret == NULL) return 1;
    ...
    ((uint32_t *)ret)[y * width + x] = ((uint32_t *)data)[y * width + w];
    ...
    and still does not work.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    4
    Thank Salem, it works now, but actually, it works with void* pointers, as it is to represent any type, that is why i needed to use as its is a dynamic array that can be uint32_t, uint64_t, or (sizeof(uint8_t)*3).

    Code:
    ...
    size_t size = sizeof(uint32_t);
    ...
    void *ret = malloc(size * width * height);
    if (ret == NULL) return 1;
    ...
    memcpy(ret + (y * width + x) * size, data + (y * width + w) * size, size);
    ...
    Last edited by Kozay Novel; 07-04-2012 at 04:27 PM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It might work with your compiler (today), but the fact remains that you can't rely on void* pointer arithmetic.
    Try increasing the warning levels on your compiler.

    $ gcc -Wall -Wextra -ansi -pedantic foo.c
    foo.c: In function ‘main’:
    foo.c:11:13: warning: pointer of type ‘void *’ used in arithmetic [-pedantic]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jul 2012
    Posts
    4
    Thanks Salem, i corrected my code now, through i normally use C99, it's a good call to remember to use, if possible, C89/90.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kozay Novel
    through i normally use C99, it's a good call to remember to use, if possible, C89/90.
    C99 does not define pointer arithmetic with void* either.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. when should I use memcpy()?
    By ppdouble in forum C Programming
    Replies: 19
    Last Post: 06-01-2012, 01:52 AM
  2. memcpy not doing its job
    By Hawkin in forum C Programming
    Replies: 4
    Last Post: 11-11-2010, 12:17 PM
  3. memcpy()
    By Moni in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2006, 05:31 AM
  4. memcpy
    By mbooka in forum C Programming
    Replies: 10
    Last Post: 02-28-2006, 02:25 AM
  5. memcpy use?
    By jerrykelleyjr in forum C++ Programming
    Replies: 21
    Last Post: 02-17-2005, 07:39 AM

Tags for this Thread