memmove_s、wmemmove_s

移动一缓冲区到另一种缓冲区。 memmove、wmemmove 的一些版本提供安全增强功能(如 CRT 中的安全功能所述)。

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 
);

参数

  • dest
    目标对象。

  • numberOfElements
    目标缓冲区的大小。

  • src
    源对象。

  • count
    复制 (memmove_s) 的字节数或字符 (wmemmove_s)。

返回值

如果成功,则为零;如果失败,返回错误代码。

错误情况

dest

numberOfElements

src

返回值

dest 的内容

NULL

any

any

EINVAL

未修改

any

any

NULL

EINVAL

未修改

any

< count

any

ERANGE

未修改

备注

复制 count 字节来自从 src 到 dest。如果源区的某些区域与目标的重叠,memmove_s 确保了在重叠区域中的原始源字节被覆盖之前被复制。

如果 dest 或 src 是空指针,或者如果目标字符串太小,则这些函数调用无效参数处理程序,如 参数验证 中所述。 如果允许执行继续,则这些函数返回 EINVAL 并将 errno 设置为 EINVAL。

要求

例程

必需的标头

memmove_s

<string.h>

wmemmove_s

<wchar.h>

有关其他兼容性信息,请参见“简介”中的兼容性

示例

// 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

.NET Framework 等效项

System::Buffer::BlockCopy

请参见

参考

缓冲区操作

_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