As implied in the title, I wanted to move a block of data around in memory, should memcpy
or memmove
be utilized? For this particular application, does it make a difference?
Asked
Active
Viewed 480 times
-1

nice_remark
- 325
- 3
- 12
-
I'm voting to close this question as off-topic because it shows no effort, and is a duplicate of numerous other questions. See [Google search results](https://www.google.com/search?q=memcpy+memmove). – Andrew Henle Jun 12 '18 at 12:44
-
See: https://stackoverflow.com/questions/28623895/why-is-memmove-faster-than-memcpy and https://stackoverflow.com/questions/1201319/what-is-the-difference-between-memmove-and-memcpy – Jonny Schubert Jun 12 '18 at 12:45
-
@AndrewHenle It's not off topic but it is a slam dunk duplicate of the second question that you linked. – JeremyP Jun 12 '18 at 12:50
1 Answers
-2
I believe the difference is that memmove
puts the text in a buffer before trying to write, and memcpy
doesn't. Thus, memcpy
is faster if the place you're moving the block to doesn't overlap with where it was to start with, while memmove
is safer if you are going to overlap.

Green Cloak Guy
- 23,793
- 4
- 33
- 53
-
ok thank you for the clarification. since this application is embedded and size/speed is a constraint `memmove` will be the way to go. even if this data is not considered text, more like a random assortment of numbers, does that matter? – nice_remark Jun 12 '18 at 12:43
-
8This is completely wrong. `memmove` is safe for overlap, and `memcpy` is not. And the C standard does document that `memmove` is performed **as if** the bytes were first copied into a temporary array, but that is not how the routine must be actually implemented. (A more likely implementation would be to choose whether to start copying from the beginning or the end depending on which address were greater.) The performance difference is usually trivial. – Eric Postpischil Jun 12 '18 at 12:44
-
@EricPostpischil thanks, I got that backwards. The format of the data shouldn't matter, as long as it's bytes of some sort. – Green Cloak Guy Jun 12 '18 at 13:09
-
`memmove()` does not need (or in practice use) a buffer; only the start addresses of the source and destination need to be compared, and if the source address < destination address, then the copy must be done in reverse from the end of the block backward, otherwise from the start, forward. The only overhead therefore is a address order evaluation. – Clifford Jun 12 '18 at 16:05