memmove copies bytes between memory regions even when source and destination overlap. Unlike memcpy, it makes no non-overlap assumption, so it is the safer interface for overlapping moves. section 13.1.1 section 15.2
Moving an overlapping buffer segment
During circular-buffer resizing, one wrapped chunk may need to move toward the new end of the allocation. The code selects memcpy when the regions cannot overlap, but uses memmove when they may overlap: memmove(ntab + nstart, ntab + ostart, ulen * sizeof(double)); The move reorganizes the contents before the resized buffer is finalized. The destination and source may refer to overlapping portions of ntab, so memmove must preserve the bytes being transferred. section 13.1.1
TAKEAWAY
- Choose memcpy when source and destination cannot overlap; choose memmove when they may overlap or may be equal. section 13.1.1 section 15.2
- Accept lower optimization freedom in exchange for a more general copy operation that handles overlapping regions. section 15.2
1 void* memcpy(void*restrict s1, void const*restrict s2, size_t n);
2 void* memmove(void* s1, const void* s2, size_t n);
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":1}],[{\"line\":1,\"ch\":25},{\"line\":1,\"ch\":26}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":8}],[{\"line\":1,\"ch\":4},{\"line\":1,\"ch\":8}],[{\"line\":0,\"ch\":10},{\"line\":0,\"ch\":16}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":8}],[{\"line\":1,\"ch\":4},{\"line\":1,\"ch\":8}],[{\"line\":0,\"ch\":22},{\"line\":0,\"ch\":30}],[{\"line\":0,\"ch\":35},{\"line\":0,\"ch\":45}],[{\"line\":0,\"ch\":22},{\"line\":0,\"ch\":30}],[{\"line\":0,\"ch\":60},{\"line\":0,\"ch\":66}],[{\"line\":1,\"ch\":44},{\"line\":1,\"ch\":50}],[{\"line\":0,\"ch\":56},{\"line\":0,\"ch\":57}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":1}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":8}],[{\"line\":1,\"ch\":4},{\"line\":1,\"ch\":8}],[{\"line\":1,\"ch\":10},{\"line\":1,\"ch\":17}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":8}],[{\"line\":1,\"ch\":4},{\"line\":1,\"ch\":8}],[{\"line\":1,\"ch\":28},{\"line\":1,\"ch\":38}],[{\"line\":0,\"ch\":60},{\"line\":0,\"ch\":66}],[{\"line\":1,\"ch\":44},{\"line\":1,\"ch\":50}]]"}
!@%STYLE%@!
Compare the declarations of memcpy and memmove, including memcpy's restrict-qualified parameters and memmove's unrestricted pointers. section 15.2

