Thread: Copying bytes to the same array at different intervals with possible overlap.

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    Surin, Thailand
    Posts
    41

    Copying bytes to the same array at different intervals with possible overlap.

    How would one write a memmove-like function that copies bytes from one location to another, but at different intervals with either positive or negative values, and... without using a temporary buffer.

    For example:
    Code:
    char *my_memmove(char *dst, char *src, size_t num, int dst_interval, int src_interval);
    
    char str[11] = " A B C D E";
    
    my_memmove(src + 9, src + 1, 5, -1, 2);
    would result in the contents of str to be
    Code:
    "     EDCBA"
    I'm not asking anyone to write me a complete function (although that would be very kind of you,) just a link to a page or a basic idea that'll at least get me started into the right direction.

    Thank you.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Why would you want to?

    In any event, the technique would involve a loop. Before entering the loop, check for overlap. If there is overlap then (depending on the manner of overlap) you may need to loop in reverse order.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    Surin, Thailand
    Posts
    41
    Yes, detecting for overlap would be the first check, however even during an overlap characters may occupy all, some or none of the overlapping array indexes.
    Code:
    str[11] = "A  B  C   ";
    
    my_memmove(str + 2, str, 3, 2, 2); 
    
    printf("\"%s\"\n", str)
    
    "  A  B  C "

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Why is this the result of your example.
    Code:
    "     EDCBA"
    Since following the directions you stated should have gotten this

    Code:
    " A B EDCBA"
    Tim S.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Location
    Surin, Thailand
    Posts
    41
    Quote Originally Posted by stahta01 View Post
    Why is this the result of your example.
    Code:
    "     EDCBA"
    Since following the directions you stated should have gotten this

    Code:
    " A B EDCBA"
    Tim S.
    Sorry, Tim. Thanks.

    I should also mention that the contents of src may be destroyed, if need be.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You and Tim agree that the answer should be
    Code:
    " A B EDCBA"
    but if this copy is supposed to happen in-place, you would overwrite 'D' in the original string with 'C', on the third character you copy, then when you try to copy the fourth, you would be copying that new 'C' and end up with:
    Code:
    " A B ACCBA"
    If, however, you don't want that behavior, you simply need to use a temp array.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Location
    Surin, Thailand
    Posts
    41
    Quote Originally Posted by anduril462 View Post
    If, however, you don't want that behavior, you simply need to use a temp array.
    With intervals of -1 and 2, a source string that has four spaces in between five characters is nearly twice as wide as the destination. Could one copy just those characters which do not have similar locations first, then use the copied character's locations (what's already been copied to src) as a temporary buffer in itself?

    Again, I'm not concerned about the original contents of the source data. It can be totally mangled. What's important is the final product.
    Last edited by Todd_65536; 10-12-2011 at 10:50 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just checking: is this a homework assignment?
    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

  9. #9
    Registered User
    Join Date
    Oct 2011
    Location
    Surin, Thailand
    Posts
    41
    Quote Originally Posted by laserlight View Post
    Just checking: is this a homework assignment?
    No... The project involves microcontrollers with 4K RAM.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Todd_65536 View Post
    With an interval of -1 and 2, a string that has four spaces in between five characters is nearly twice as wide as the destination. Could one copy just those characters which do not have common addresses first, then use the copied character's locations (what's already been copied to src) as a temporary buffer in itself?
    Yes, one could, but one would not want to add that much complexity in address calculations, etc, simply to avoid using a temp array. Memory is cheap, make a temp array. It only needs to have room for num bytes.

    Again, I'm not concerned about the original contents of the source data. It can be totally mangled. What's important is the final product.
    I understand your implementation can alter src. My point was that, given the overlapping condition, you may alter parts of src before they're copied so that dst will no longer appear to contain a copy of src at the specified interval, but instead some arbitrary combination of src characters. So, what should the final product really be in your example?
    Code:
    " A B EDCBA"
    or
    Code:
    " A B ACCBA"
    You must pick one. It will determine how you implement this.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Todd_65536
    No... The project involves microcontrollers with 4K RAM.
    Ah. I asked this as an indirect way of asking "has someone solved, or purportedly solved, this problem?"

    This is what the C standard has to say about memmove:
    Quote Originally Posted by C99 Clause 7.21.2.2 Paragraph 2
    The memmove function copies n characters from the object pointed to by s2 into the object pointed to by s1. Copying takes place as if the n characters from the object pointed to by s2 are first copied into a temporary array of n characters that does not overlap the objects pointed to by s1 and s2, and then the n characters from the temporary array are copied into the object pointed to by s1.
    My interpretation of "as if" is that while the standard does not mandate the implementation, what it describes is the expected implementation. Consequently, I wonder if this problem actually cannot be solved within the given constraints and with well defined behaviour.

    Consider even grumpy's suggestion from post #2: how would you check for overlap? This would require pointer comparison, but pointer comparison results in undefined behaviour if the pointers point to elements in different arrays. Thus, you can only safely do pointer comparison if you define this memmove substutite as valid for moving bytes within an array, not between arrays.
    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

  12. #12
    Registered User
    Join Date
    Oct 2011
    Location
    Surin, Thailand
    Posts
    41
    Quote Originally Posted by anduril462 View Post
    Yes, one could, but one would not want to add that much complexity in address calculations, etc, simply to avoid using a temp array. Memory is cheap, make a temp array. It only needs to have room for num bytes.


    I understand your implementation can alter src. My point was that, given the overlapping condition, you may alter parts of src before they're copied so that dst will no longer appear to contain a copy of src at the specified interval, but instead some arbitrary combination of src characters. So, what should the final product really be in your example?
    Code:
    " A B EDCBA"
    or
    Code:
    " A B ACCBA"
    You must pick one. It will determine how you implement this.
    The final contents of str should be this:

    Code:
    "? ? EDCBA"
    Where '?' can be anything. Free RAM is not cheap, whether we're dealing with microcontrollers with very limited space or, large individual objects. Memory is cheap until it isn't.

    I'm not asking for a replacement to memmove, I'm asking for help with an algorithm that can shuffle data without a temporary buffer. I'll accept a large number of pointer comparisons (or index comparisons) even if it means a drastic reduction in execution speed.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Todd_65536 View Post
    I'm asking for help with an algorithm that can shuffle data without a temporary buffer.
    How much space are you talking about here? What is your expected "biggest overlap"? Two pointers is say eight bytes. Can you just use an eight byte buffer instead for segments that over lap?


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Oct 2011
    Location
    Surin, Thailand
    Posts
    41
    Quote Originally Posted by laserlight View Post
    Thus, you can only safely do pointer comparison if you define this memmove substutite as valid for moving bytes within an array, not between arrays.
    For the sake of clarity, my_memmove results in undefined behavior if *dst and *src are in different arrays. For now, I'm only concerned with manipulating a lone series of bytes.

  15. #15
    Registered User
    Join Date
    Oct 2011
    Location
    Surin, Thailand
    Posts
    41
    Quote Originally Posted by quzah View Post
    How much space are you talking about here? What is your expected "biggest overlap"? Two pointers is say eight bytes.
    The worst case biggest overlap scenario would equal the size of source and destination, for example, reversing a series of a billion bytes in a machine with large available RAM...

    Code:
    my_memmove(str, str + (1 << 30) - 1, (1 << 30), 1, -1);
    I would imagine, too, that if this condition were to indeed occur with perhaps src_interval and dst_interval being equal, then the algorithm might resemble that of memmove().

    Can you just use an eight byte buffer instead for segments that over lap?

    Quzah.
    An eight byte buffer doesn't sound like much, does it?
    Last edited by Todd_65536; 10-12-2011 at 08:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Connecting intervals together
    By kaspari22 in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2011, 11:32 AM
  2. Replies: 3
    Last Post: 04-26-2011, 01:42 AM
  3. need help in copying unsigned char array to bit array
    By lovesunset21 in forum C Programming
    Replies: 8
    Last Post: 10-29-2010, 07:23 AM
  4. Memory overlap in function call
    By wots_guge in forum C Programming
    Replies: 5
    Last Post: 06-06-2006, 03:22 AM
  5. making a absolute overlapped window, even overlap games
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-22-2004, 08:53 AM

Tags for this Thread