diff memcpy and memmove

Discussion in 'C Programming' started by novice, Mar 10, 2006.

  1. novice

    novice Guest

    Please explain with an example whts the
    DIFFERENCE between "memcpy" and "memmove"
    novice, Mar 10, 2006
    #1
    1. Advertising

  2. "novice" <> writes:
    > Please explain with an example whts the
    > DIFFERENCE between "memcpy" and "memmove"


    memcpy() invokes undefined behavior if it attempts to copy between
    objects that overlap. memmove() works as if by copying the source
    object to a temporary array and then copying the temporary array to
    the target object (though it probably will do the copy directly, being
    careful about the order in which bytes are copied).

    This should be explained in your textbook, your system's
    documentation, or by a quick Google search.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Mar 10, 2006
    #2
    1. Advertising

  3. On Friday 10 March 2006 06:36, novice opined (in
    <>):

    > Please explain with an example whts the
    > DIFFERENCE between "memcpy" and "memmove"


    Smells like homework assignment. Make an effort, and DIY.

    Here's one: "memcpy" is a string literal that takes 6 bytes (one for
    each letter, and one for terminating '\0'), and "memmove" takes 7.

    And here's what the Standard has to say about the functions that go by
    the names of `memcpy` and `memmove`:

    7.21.2.1 The memcpy function
    Synopsis
    #include <string.h>
    void *memcpy(void * restrict s1,
    const void * restrict s2,
    size_t n);
    Description
    The memcpy function copies n characters from the object pointed to by
    s2 into the object pointed to by s1. If copying takes place between
    objects that overlap, the behavior is undeï¬ned.

    Returns
    The memcpy function returns the value of s1.

    7.21.2.2 The memmove function
    Synopsis
    #include <string.h>
    void *memmove(void *s1, const void *s2, size_t n);
    Description
    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 ï¬rst 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.

    Returns
    The memmove function returns the value of s1.

    In short, use `memmove` when you're not sure that the memory regions do
    not overlap. You can surely come up with an example yourself.

    (Also, no need to SHOUT at us.)

    --
    BR, Vladimir

    Langsam's Laws:
    (1) Everything depends.
    (2) Nothing is always.
    (3) Everything is sometimes.
    Vladimir S. Oka, Mar 10, 2006
    #3
  4. On Friday 10 March 2006 06:36, novice opined (in
    <>):

    > Please explain with an example whts the


    I don't know what `whts` means so my previous reply may have been
    completely wrong. Please do not use silly abrvtns.

    > DIFFERENCE between "memcpy" and "memmove"


    --
    BR, Vladimir

    QOTD:
    All I want is more than my fair share.
    Vladimir S. Oka, Mar 10, 2006
    #4
  5. On Friday 10 March 2006 07:11, Vladimir S. Oka opined (in
    <dur8qa$5it$-infra.bt.com>):

    > On Friday 10 March 2006 06:36, novice opined (in
    > <>):
    >
    >> Please explain with an example whts the
    >> DIFFERENCE between "memcpy" and "memmove"

    >
    > Smells like homework assignment. Make an effort, and DIY.
    >
    > Here's one: "memcpy" is a string literal that takes 6 bytes (one for
    > each letter, and one for terminating '\0'), and "memmove" takes 7.


    Too early in the morning. Should've said 7 and 8 respectively. :-(

    --
    BR, Vladimir

    For my birthday I got a humidifier and a de-humidifier. I
    put them in the same room and let them fight it out.
    -- Steven Wright
    Vladimir S. Oka, Mar 10, 2006
    #5
  6. In article <dur904$eat$-infra.bt.com>,
    Vladimir S. Oka <> wrote:

    >> Please explain with an example whts the


    >I don't know what `whts` means so my previous reply may have been
    >completely wrong. Please do not use silly abrvtns.


    Presumably it was a typo for "whats", which in turn should have been
    "what's".

    -- Richard
    Richard Tobin, Mar 10, 2006
    #6
  7. "Richard"posted the following on 2006-03-10:

    > In article <dur904$eat$-infra.bt.com>,
    > Vladimir S. Oka <> wrote:
    >
    >>> Please explain with an example whts the

    >
    >>I don't know what `whts` means so my previous reply may have been
    >>completely wrong. Please do not use silly abrvtns.

    >
    > Presumably it was a typo for "whats", which in turn should have been
    > "what's".
    >
    > -- Richard


    Holy cow. That again? Surely anyone can see that that was a
    typo and not an "abrvn" :-;

    --
    "A desk is a dangerous place from which to view the world" - LeCarre.
    Richard G. Riley, Mar 10, 2006
    #7
  8. "novice"posted the following on 2006-03-10:

    > Please explain with an example whts the
    > DIFFERENCE between "memcpy" and "memmove"
    >


    Its a question of whether the memory areas overlap. If they do, then
    you should use memmove.

    --
    "A desk is a dangerous place from which to view the world" - LeCarre.
    Richard G. Riley, Mar 10, 2006
    #8
  9. novice

    Default User Guest

    novice wrote:

    > Please explain with an example whts the
    > DIFFERENCE between "memcpy" and "memmove"


    Always check the FAQs before posting questions of this nature:

    http://c-faq.com/ansi/memmove.html


    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, Mar 10, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    11,358
    Gianni Mariani
    Feb 14, 2005
  2. Mike Wahler

    resolved: difference between memcpy and memmove

    Mike Wahler, Sep 27, 2003, in forum: C Programming
    Replies:
    21
    Views:
    1,422
    Jirka Klaue
    Sep 30, 2003
  3. somenath

    Question regarding memcpy and memmove function

    somenath, Sep 2, 2007, in forum: C Programming
    Replies:
    16
    Views:
    775
    somenath
    Sep 4, 2007
  4. Noob

    Re: memcpy and memmove

    Noob, Apr 3, 2013, in forum: C Programming
    Replies:
    0
    Views:
    164
  5. Malcolm McLean

    Re: memcpy and memmove

    Malcolm McLean, Apr 3, 2013, in forum: C Programming
    Replies:
    0
    Views:
    167
    Malcolm McLean
    Apr 3, 2013
Loading...

Share This Page