Interview Questions and Answers :: Alcatel-Lucent
Home > Experience Archives > Alcatel-Lucent > Interview Question Set 1 > Discussion
1. Can you write a program to implement memcpy. copy must be successful even source and destination address overlap.
Answer:
As given copy should be successful even source and dest address overlap, than we should implement memmove functionality
void ownMemMove(void *dest, void *src, size_t n)
{
char *src_l = (char *)src;
char *dest_l = (char *)dest;
char *temp [n];
memset(temp, '\0', n);
for (int i=0; i < n; i++)
temp[i] = src_l[i];
for (int i=0; i < n; i++)
dest_l[i] = temp[i];
}
Aman
25 Aug, 2016 4:13 PM
As given copy should be successful even source and dest address overlap, than we should implement memmove functionality
void ownMemMove(void *dest, void *src, size_t n)
{
char *src_l = (char *)src;
char *dest_l = (char *)dest;
char *temp [n];
memset(temp, '\0', n);
for (int i=0; i < n; i++)
temp[i] = src_l[i];
for (int i=0; i < n; i++)
dest_l[i] = temp[i];
}
void ownMemMove(void *dest, void *src, size_t n)
{
char *src_l = (char *)src;
char *dest_l = (char *)dest;
char *temp [n];
memset(temp, '\0', n);
for (int i=0; i < n; i++)
temp[i] = src_l[i];
for (int i=0; i < n; i++)
dest_l[i] = temp[i];
}
Report Error
Report Error
Please Login First Click Here