memmove_s, wmemmove_s

Moves one buffer to another. These functions are versions of memmove, wmemmove with security enhancements as described in Security features in the CRT.

Syntax

errno_t memmove_s(
   void *dest,
   size_t numberOfElements,
   const void *src,
   size_t count
);
errno_t wmemmove_s(
   wchar_t *dest,
   size_t numberOfElements,
   const wchar_t *src,
   size_t count
);

Parameters

dest
Destination object.

numberOfElements
Size of the destination buffer.

src
Source object.

count
Number of bytes (memmove_s) or characters (wmemmove_s) to copy.

Return value

Zero if successful; an error code on failure

Error conditions

dest numberOfElements src Return value Contents of dest
NULL any any EINVAL not modified
any any NULL EINVAL not modified
any < count any ERANGE not modified

Remarks

Copies count bytes of characters from src to dest. If some portions of the source and the destination regions overlap, memmove_s ensures that the original source bytes in the overlapping region are copied before being overwritten.

If dest or if src is a null pointer, or if the destination string is too small, these functions invoke an invalid parameter handler, as described in Parameter validation . If execution is allowed to continue, these functions return EINVAL and set errno to EINVAL.

By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT.

Requirements

Routine Required header
memmove_s <string.h>
wmemmove_s <wchar.h>

For more compatibility information, see Compatibility.

Example

// crt_memmove_s.c
//
// The program demonstrates the
// memmove_s function which works as expected
// for moving overlapping regions.

#include <stdio.h>
#include <string.h>

int main()
{
   char str[] = "0123456789";

   printf("Before: %s\n", str);

   // Move six bytes from the start of the string
   // to a new position shifted by one byte. To protect against
   // buffer overrun, the secure version of memmove requires the
   // the length of the destination string to be specified.

   memmove_s((str + 1), strnlen(str + 1, 10), str, 6);

   printf_s(" After: %s\n", str);
}

Output

Before: 0123456789
After: 0012345789

See also

Buffer manipulation
_memccpy
memcpy, wmemcpy
strcpy_s, wcscpy_s, _mbscpy_s
strcpy, wcscpy, _mbscpy
strncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l, _mbsncpy_s, _mbsncpy_s_l
strncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l