/***
*memmove - Copy source buffer to destination buffer
*
*Purpose:
* memmove() copies a source memory buffer to a destination memory buffer.
* This routine recognize overlapping buffers to avoid propogation.
* For cases where propogation is not a problem, memcpy() can be used.
*
*Entry:
* void *dst = pointer to destination buffer
* const void *src = pointer to source buffer
* size_t count = number of bytes to copy
*
*Exit:
* Returns a pointer to the destination buffer
*
*Exceptions:
*******************************************************************************/
void * __cdecl mymemcpy (
void * dst,
const void * src,
size_t count
)
{
void * ret = dst;
/*
* copy from lower addresses to higher addresses
*/
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst + 1;
src = (char *)src + 1;
}
return(ret);
}
void * __cdecl mymemmove (
void * dst,
const void * src,
size_t count
)
{
void * ret = dst;
if (dst <= src || (char *)dst >= ((char *)src + count)) {
/*
* Non-Overlapping Buffers
* copy from lower addresses to higher addresses
*/
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst + 1;
src = (char *)src + 1;
}
}
else {
/*
* Overlapping Buffers
* copy from higher addresses to lower addresses
*/
dst = (char *)dst + count - 1;
src = (char *)src + count - 1;
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst - 1;
src = (char *)src - 1;
}
}
return(ret);
}
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0;
int a[10];
for(i; i < 10; i++)
{
a[i] = i;
}
mymemcpy(&a[4], a, sizeof(int)*6);
for(i = 0; i < 10; i++)
{
printf("%d ",a[i]);
}
printf("\n");
for(i=0; i < 10; i++)
{
a[i] = i;
}
mymemmove(&a[4], a, sizeof(int)*6);
for(i = 0; i < 10; i++)
{
printf("%d ",a[i]);
}
printf("\n");
return 0;
}
Result:
0 1 2 3 0 1 2 3 0 1
0 1 2 3 0 1 2 3 4 5