diff memcpy and memmove

K

Keith Thompson

novice said:
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.
 
V

Vladimir S. Oka

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.
 
V

Vladimir S. Oka

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.
 
V

Vladimir S. Oka

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
 
R

Richard Tobin

Please explain with an example whts the
[/QUOTE]
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
 
R

Richard G. Riley

"Richard"posted the following on 2006-03-10:
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[/QUOTE]

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

Richard G. Riley

"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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top