将一个缓冲区移到另一个缓冲区。 这些函数的版本是 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。
默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态。
要求
| 例程 | 必需的标头 |
|---|---|
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);
}
输出
Before: 0123456789
After: 0012345789