liveBook · Manning

Memmove

As explained in Modern C

memmove is a C standard library function that copies bytes between memory regions while permitting the regions to overlap.

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

How Modern C covers it

How deep it goes
Explains the overlap rule and demonstrates selection against memcpy, but defers the function's implementation internals.
Examples
Uses a wrapped circular-buffer chunk moved upward with memmove after reallocating storage.
What it assumes
Assumes pointers, memory regions, realloc, and aliasing while distinguishing memcpy's non-overlap requirement.
What you can do after
Apply it via the C standard library when source and destination memory regions may overlap.
How it connects
Connects memmove with memcpy, restrict qualifiers, aliasing, realloc, and circular buffers.

Also explained in

Compare how 2 books explain memmove →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.