Thread: need urgent help in memcopy function

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    37

    need urgent help in memcopy function

    Hi everyone,

    I am a dummy in C programming. I have a problem with copying the array of 64 elements of unsigned char to array of 8 element of uint64_t. For example:

    unsigned char p[64]
    uint64_t Data[8];

    memcpy (&Data, &p, 64);


    I am not sure if it is right. Please help me. Thanks a lot.

    Moreover, do both p and Data have 64 bytes?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    memcpy(Data, p, sizeof(Data) > sizeof(p) ? sizeof(p) : sizeof(Data));
    Arrays are implicitly converted to pointers to their first element when passing them to functions.
    And avoid "urgent" text in titles next time. Post your problem in time instead.

    Also, sizeof(Data) > sizeof(p) ? sizeof(p) : sizeof(Data) is a shorthand notification for an if statement. It will return whichever of sizeof(Data) and sizeof(p) that is smaller, to avoid buffer overruns.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    A char is a byte in C per definition (but it does not need to have 8 bits). uint64_t is exactly 64 bits on platforms where such a type can be implemented. Because of that, your solution is not portable. For example, a char could have 16 bits which would mean that a byte on that system also has 16 bits. That means 64 bytes is actually 16 * 64 bits which is more than the Data array can contain.

    &Data is the address of the array, and you should be using either Data or &Data[0] if you want a pointer to the first element. The memory address is the same, but the types are not.

    Another problem is that you need to know the byte order if you want to do anything meaningful with the uint64_t variable once you've copied the chars into it.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Thank you so much. One more question is how to convert an uint64_t n to an unint64_t array of 8 elements. I tried it with memcpy:

    unint64_t Data[8];
    memcpy(&Data[0], &n, sizeof(Data))

    And then, I tried to print the Data array

    if n is 0, it is ok, it printed 00000000
    but if n is 1, it is 11111111. It should be 00000001.

    Any suggestion? Thank you.
    Last edited by lovesunset21; 10-17-2010 at 03:44 PM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Define "convert" in this context.
    Also, I think you fail to understand how memcpy works. The third parameter tells the function how many bytes to copy from source to dest. If source is not 8 * 8 bytes, then I wonder what will happen?
    Last edited by Elysia; 10-17-2010 at 03:31 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by lovesunset21 View Post
    Thank you so much. One more question is how to convert an integer n to an unint64_t array of 8 elements. I tried it with memcpy:

    unint64_t Data[8];
    memcpy(&Data[0], &n, sizeof(Data))

    And then, I tried to print the Data array

    It displayed "Segmentation Fault".


    Any suggestion? Thank you.
    The problem is that memcpy will try to copy sizeof(...) bytes from src into dest. Clearly the seg fault is because you can't copy after the 4th byte (I am assuming 32-bit ints here) from n, so when memcpy tries to address the 5th byte you overstep your bounds and seg fault.

    Try
    Code:
    memcpy(&Data[0], &n, sizeof(int));
    Also, if you are getting "segmentation fault" rather than "access violation" that leads me to think you're not on Windows, meaning you have man pages. You can learn a lot a lot alot from man pages. Try
    Code:
    man memcpy
    Type q to quit. Scroll with the arrow keys.
    Last edited by QuadraticFighte; 10-17-2010 at 03:35 PM.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    > One more question is how to convert an integer n to an unint64_t array of 8 elements.

    That doesn't make any sense, and you still have the same problems with portability as I described above. Either way, copying sizeof(Data) number of bytes from the address &n, where n is an integer, will result in you reading in additional garbage and possible trying to access memory you don't have access to (hence the segfault). Think about how many bytes an integer is and how many bytes you're trying to copy when you specify sizeof(Data) as the number of bytes to copy.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Thank you so much for your quick replies.


    I need to copy an integer n (or maybe an uint64_t) to an uint64_t array of 8 elements.
    For example, if n is 0, the array will be 0 0 0 0 0 0 0 0
    if n is 1 then, it will be 0 0 0 0 0 0 0 00000001.

    I tried memcpy, but it seems impossible. Can you give me a hint. Thank you so much.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You mean that the last index in the array shall hold the value of the integer and the rest be 0?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Yes, that 's right. And it will increase depend on n. Any idea? Thanks.

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Actually, I have to encrypt a counter with three fish algorithm. Therefore, I need to convert it into the array of uint64_t.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    memset(your_array, 0), sizeof(your_array) - sizeof(your_array[0]));
    your_array[sizeof(your_array) - sizeof(your_array[0])] = n;
    You could also use a loop to set index 0...n-1 to 0, and then assign your integer to n.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 07:42 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM