std::memmove - cppreference.com

From cppreference.com

void*memmove(void*dest,constvoid*src,std::size_tcount);Performs the following operations in order:

Copies count characters (as if of type unsignedchar, the same below) from the object pointed to by src into a temporary array arr of count characters, where arr does not overlap the objects pointed to by dest and src.

Implicitly creates

objects at dest.

Copies count characters from arr into the object pointed to by dest.

If dest or src is a

null pointer

or

invalid pointer

, the behavior is undefined.

Parameters

dest - pointer to the memory location to copy to src - pointer to the memory location to copy from count - number of bytes to copy Return value

If there is a

suitable created object

, returns a pointer to it; otherwise returns dest.

Notes

Despite the specification says a temporary buffer is used, actual implementations of this function do not incur the overhead of double copying or extra memory. 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 to

std::memcpy

when there is no overlap at all.

Where

strict aliasing

prohibits examining the same memory as values of two different types, std::memmove may be used to convert the values.

Example

Run this code

#include<cstring>#include<iostream>intmain(){charstr[]="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 Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior

LWG 4064

C++98 it was unclear whether the returned pointer points to a suitable created object made clear See also

memcpy

copies one buffer to another
(function)

[edit]

memsetmemset_explicit

(C++26)

fills a buffer with a character
(function)

[edit]

wmemmove

copies a certain amount of wide characters between two, possibly overlapping, arrays
(function)

[edit]

copycopy_if

(C++11)

copies a range of elements to a new location
(function template & algorithm function object)

[edit]

ranges::copyranges::copy_if

(C++20)(C++20)

copy_backward

copies a range of elements in backwards order
(function template & algorithm function object)

[edit]

ranges::copy_backward

(C++20)

is_trivially_copyable

(C++11)

checks if a type is trivially copyable
(class template)

[edit]

C documentation

for memmove