バッファー間でバイトをコピーします。 これらの関数は、「CRT のセキュリティ機能」で説明されているように、セキュリティが強化されたバージョンの memcpy、wmemcpy です。
構文
errno_t memcpy_s(
void *dest,
size_t destSize,
const void *src,
size_t count
);
errno_t wmemcpy_s(
wchar_t *dest,
size_t destSize,
const wchar_t *src,
size_t count
);
パラメーター
dest
コピー先のバッファー。
destSize
コピー先バッファーのサイズ。memcpy_s の場合はバイト数、wmemcpy_s の場合はワイド文字の数 (wchar_t)。
src
コピー元のバッファー。
count
コピーする文字数。
戻り値
正常終了した場合は 0 を返します。失敗した場合はエラー コードを返します。
エラー条件
dest |
destSize |
src |
count |
戻り値 | dest の内容 |
|---|---|---|---|---|---|
| 任意 | 任意 | 任意 | 0 | 0 | Not modified |
NULL |
任意 | 任意 | 0 以外 | EINVAL |
Not modified |
| 任意 | 任意 | NULL |
0 以外 | EINVAL |
dest は 0 に設定されます |
| 任意 | < count |
任意 | 0 以外 | ERANGE |
dest は 0 に設定されます |
解説
memcpy_s は、count から src に dest バイトをコピーし、wmemcpy_s は count 個のワイド文字をコピーします。 コピー元とコピー先の領域が重なり合う場合の memcpy_s の動作は未定義です。 重なり合う領域を処理するには、memmove_s を使用します。
これらの関数では、パラメーターの検証が行われます。 countが 0 以外で、destまたはsrcが null ポインターである場合、またはdestSizeがcountよりも小さい場合は、「パラメーター検証で説明されているように、これらの関数は無効なパラメーター ハンドラーを呼び出します。 実行を続行できる場合、これらの関数は EINVAL または ERANGEを返し、戻り値に errno を設定します。
既定では、この関数のグローバル状態の適用対象は、アプリケーションになります。 この動作を変更するには、「CRT でのグローバル状態」を参照してください。
要件
| ルーチンによって返される値 | 必須ヘッダー |
|---|---|
memcpy_s |
<memory.h> または <string.h> |
wmemcpy_s |
<wchar.h> |
互換性の詳細については、「 Compatibility」を参照してください。
例
// crt_memcpy_s.c
// Copy memory in a more secure way.
#include <memory.h>
#include <stdio.h>
int main()
{
int a1[10], a2[100], i;
errno_t err;
// Populate a2 with squares of integers
for (i = 0; i < 100; i++)
{
a2[i] = i*i;
}
// Tell memcpy_s to copy 10 ints (40 bytes), giving
// the size of the a1 array (also 40 bytes).
err = memcpy_s(a1, sizeof(a1), a2, 10 * sizeof (int) );
if (err)
{
printf("Error executing memcpy_s.\n");
}
else
{
for (i = 0; i < 10; i++)
printf("%d ", a1[i]);
}
printf("\n");
}
0 1 4 9 16 25 36 49 64 81
関連項目
バッファー操作
_memccpy
memchr, wmemchr
memcmp, wmemcmp
memmove, wmemmove
memset, wmemset
strcpy、 wcscpy、 _mbscpy
strncpy、 _strncpy_l、 wcsncpy、 _wcsncpy_l、 _mbsncpy、 _mbsncpy_l
strncpy_s、 _strncpy_s_l、 wcsncpy_s、 _wcsncpy_s_l、 _mbsncpy_s、 _mbsncpy_s_l