Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By laserlight

Help memcpy!

This is a discussion on Help memcpy! within the C Programming forums, part of the General Programming Boards category; Hi all, this is my first post and i'm a beginner in c programming, i need help to understand what ...

  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,675
    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,675
    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
    2,516
    My guess is (y * width + x ) * size is closer to [y * width + x].

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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]
    std10093 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    21,448
    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.
    Salem likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 11:17 AM
  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, 01:25 AM
  5. memcpy use?
    By jerrykelleyjr in forum C++ Programming
    Replies: 21
    Last Post: 02-17-2005, 06:39 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21