Sto cercando di capire la differenza tra memcpy()e memmove(), e ho letto il testo che memcpy()non si occupa della fonte e della destinazione sovrapposte memmove(), invece.
Tuttavia, quando eseguo queste due funzioni su blocchi di memoria sovrapposti, entrambi danno lo stesso risultato. Ad esempio, prendere il seguente esempio MSDN nella memmove()pagina della guida: -
C'è un esempio migliore per comprendere gli svantaggi memcpye come memmoverisolverlo?
// 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 );
}
Produzione:
The string: aabbcc
New string: aaaabb
The string: aabbcc
New string: aaaabb
memcpysarebbe assertche le regioni non si sovrappongano piuttosto che nascondere intenzionalmente bug nel tuo codice.
The string: aabbcc New string: aaaaaa The string: aabbcc New string: aaaabb