memcpy()
との違いを理解しようとしていますが、重複するソースと宛先を処理しないmemmove()
テキストを読みました。memcpy()
memmove()
ただし、重複するメモリ ブロックでこれら 2 つの関数を実行すると、どちらも同じ結果になります。たとえば、memmove()
ヘルプ ページにある次の MSDN の例を見てください。
の欠点とmemcpy
それをどのようmemmove
に解決するかを理解するためのより良い例はありますか?
// crt_memcpy.c
// Illustrate overlapping copy: memmove always handles it correctly; memcpy may handle
// it correctly.
#include <memory.h>
#include <string.h>
#include <stdio.h>
char str1[7] = "aabbcc";
int main( void )
{
printf( "The string: %s\n", str1 );
memcpy( str1 + 2, str1, 4 );
printf( "New string: %s\n", str1 );
strcpy_s( str1, sizeof(str1), "aabbcc" ); // reset string
printf( "The string: %s\n", str1 );
memmove( str1 + 2, str1, 4 );
printf( "New string: %s\n", str1 );
}
出力:
The string: aabbcc
New string: aaaabb
The string: aabbcc
New string: aaaabb