#include<stdio.h>
#include<string.h>
int main()
{
char data1[] = "0123456789";
char data2[] = "0123456789";
char data3[] = "0123456789";
char data4[] = "0123456789";
memcopy(&data1[5],&data1[3],5);
memcopy(&data2[3],&data2[5],5);
memove(&data3[5],&data3[3],5);
memove(&data4[3],&data4[5],5);
printf("MemCopy=%s\nMemCopy=%s\nMemMove=%s\nMemMove=%s\n", data1,data2, data3, data4);
return 0;
}
//Memmove implementation in C
void memove(void* destination,void* source,unsigned int amount)
{
char *dest = (char *)destination; // Converting void pointer into char pointer
char *src = (char *)source;
int loop_count=0; //for loop count
int count=0;
int flag = 0; // To check for overlapping address
//Loop to determine overlapping address
for(loop_count=0;loop_count<amount;loop_count++)
{
if((src+loop_count) == dest) // For destination first byte within source range
{
count = amount;
while(amount--)
{
*(dest+amount) = *(src+amount);
}
amount = count;
flag = 1;
}
else if((dest+loop_count) == src) // For destination last byte within source range
{
memcopy(dest,src,amount);
flag =1;
}
}
if(flag == 0) // No overlapping of addresses
memcopy(dest,src,amount);
}
//Memcpy implementation in C
void memcopy(void* destination,void* source,int amount)
{
char *dest = (char *)destination;
char *src = (char *)source;
while(amount--) // Copying from starting
{
*dest++=*src++;
}
}
No comments:
Post a Comment