memcpy() & memmove()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gsi
    New Member
    • Jul 2007
    • 51

    memcpy() & memmove()

    Hi all,
    memmove() is guranteed to work correctly if the objects overlap, I am not sure why this can't be done without any extra condition check in code for the memmove() implementation or why would memcpy() fail in this situation. Please advice.

    Thanks in advance,
    Gsi.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by gsi
    Hi all,
    memmove() is guranteed to work correctly if the objects overlap, I am not sure why this can't be done without any extra condition check in code for the memmove() implementation or why would memcpy() fail in this situation. Please advice.

    Thanks in advance,
    Gsi.
    The memcpy() method could/can be implemented using just one machine code
    instruction; for Intel processors a forward (lowest to highest memory address)
    instruction is available. This single instruction definitely can't handle overlapping
    memory areas; hence the two separate functions: memcpy and memmove.

    kind regards,

    Jos

    Comment

    • gsi
      New Member
      • Jul 2007
      • 51

      #3
      Originally posted by JosAH
      The memcpy() method could/can be implemented using just one machine code
      instruction; for Intel processors a forward (lowest to highest memory address)
      instruction is available. This single instruction definitely can't handle overlapping
      memory areas; hence the two separate functions: memcpy and memmove.

      kind regards,

      Jos
      Jos,
      That's where I am confused about , I could'nt make out why the instruction could not handle overlapping areas. I mean what it does to copy the memory right way without reversing the order of copy.

      Thanks in advance,
      Gsi.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by gsi
        Jos,
        That's where I am confused about , I could'nt make out why the instruction could not handle overlapping areas. I mean what it does to copy the memory right way without reversing the order of copy.

        Thanks in advance,
        Gsi.
        Think about it: a single machine code instruction that can copy n bytes from A
        to B, e.g. n == 3, A == 1 and B ==2 so copy byte from 1 to 2, copy byte from 2
        to 3 and bingo: you're copying the original byte that was at position 1 originally.

        A single instruction may be extremely fast but it is way too stupid to decide what
        to do in case of overlapping areas. Hence the two functions: a stupid but fast one
        and a clever but slow one. For the non-overlapping areas the memmove tries to
        be as fast as possible. Some processors even have a descending copy instruction
        so all the memmove function has to do is decide which one to use and fire up one
        of the instructions. But still it'll be (a bit) slower than the memcpy function.

        kind regards,

        Jos

        Comment

        • gsi
          New Member
          • Jul 2007
          • 51

          #5
          Jos,
          Thanks for the explanation.

          Regards,
          Gsi.

          Comment

          Working...