std::memmove(3)
π NAME
okay so bestie, meet std::memmove β the function's whole name is literally std::memmove, no cap. She's the overlapping-copy queen and she's about to eat π
β¨ SYNOPSIS β¨
First thing's first, she lives here:
Defined in header <cstring>
And here's her whole signature, look at her:
void* memmove( void* dest, const void* src, std::size_t count );
So what she does: she copies count characters from the object pointed to by src over to the object pointed to by dest. Both of those objects get reinterpreted as arrays of unsigned char β that's just the vibe, that's how she sees the world π¦
And here's the iconic part, the reason she's the main character: the objects may overlap π₯ Copying takes place as if the characters were first copied into a temporary character array, and then copied from that array into dest. Like she's got a little staging area. So chill.
β οΈ But listen. If eitherdestorsrcis an invalid or null pointer, the behavior is undefined β even ifcountis zero. Yeah. Even then. Even with zero. Not a drill π
β οΈ Also: if the objects are potentially-overlapping or not TriviallyCopyable, the behavior of memmove is not specified and may be undefined. So likeβ¦ know your types, bestie π
π PARAMETERS π
destβ pointer to the memory location to copy to (the destination, the place we're moving into)srcβ pointer to the memory location to copy from (the source, the thing we're copying)countβ number of bytes to copy
π RETURN VALUE π
She returns dest. That's it. That's the return. Short king energy but make it a pointer π«Ά
πΈ NOTES πΈ
std::memmovemay be used to implicitly create objects in the destination buffer. Slay.- Okay so this is so funny β even though she's specified "as if" a temporary buffer is used, actual implementations of this function do not incur the overhead of double copying or extra memory π
Like, she's not actually allocating your little staging array, that's just the description. For small
count, it may load up and write out registers; for larger blocks, a common approach (glibc and bsd libc) is to copy bytes forwards from the beginning of the buffer if the destination starts before the source, and backwards from the end otherwise, with a fall back tostd::memcpywhen there is no overlap at all. That's the tea β - Where strict aliasing prohibits examining the same memory as values of two different types,
std::memmovemay be used to convert the values. Iconic loophole behavior, honestly.
π§ EXAMPLE π§
// Run this code
#include <cstring>
#include <iostream>
int main()
{
char str[] = "1234567890";
std::cout << str << '\n';
std::memmove(str + 4, str + 3, 3); // copies from [4, 5, 6] to [5, 6, 7]
std::cout << str << '\n';
}
β Output:
1234567890
1234456890
See how the 1 and 2 and 3 stayed put and everything shifted? She did that. Overlapping region and all π
π SEE ALSO π
memcpyβ copies one buffer to another (function)memsetβ fills a buffer with a character (function)wmemmoveβ copies a certain amount of wide characters between two, possibly overlapping, arrays (function)copyβ copies a range of elements to a new locationcopy_ifβ (function template) (C++11)copy_backwardβ copies a range of elements in backwards order (function template)is_trivially_copyableβ (C++11) checks if a type is trivially copyable (class template)- C documentation for
memmove
okay that's the whole page, go forth and copy your overlapping bytes responsibly π
from http://cppreference.com, dated 2024.06.10, and this is std::memmove(3) signing off β mwah π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms