Contributed by jj on from the memwrite-me-an-article dept.
memcpy vs memmove
A few notes about memcpy vs memmove and some related items as well.
The C standard specifies two functions for copying memory regions, memcpy and memmove. The important difference is that it is undefined behavior to call memcpy with overlapping regions. One must use memmove for that. As the names imply, memcpy copies data from one region to another, while memmove moves data within a region. (Itís also perfectly acceptable to memmove between different regions.)
This subtle but important distinction allows memcpy to be optimized more aggressively. In the case of memmove between overlapping regions, care must be taken not to destroy the contents of the source before they are done copying. This is easiest to see with a naive implementation of a copy loop.
Read the whole thing; it's an exciting journey into the world of bug-hunting!
(Comments are closed)