Strcpy:
Strcpy deals with arrays of characters ending in \0.It will fail horribly if there is no '\0'.
On the other hand the syntax of strcpy is:
char *strcpy(char *s1, const char *s2);
s1-destination string.
s2-source string.
It will copy the string pointed by s2 into s1.
While copying is there any overlapping occures means its performance is undefined.
The disadvantage of strcpy is, It doesn't perform Bounds Checking(Setting or Verifying the size of data before storing the data into the Buffers).
For Example:
#include <stdio.h>
#include <string.h>
int main()
{
char buf[4]= "";
char sun[12]= "how are you";
strcpy(buf, sun);
printf("%s\n", buf);
return 0;
}
In the above program The size of the source is high compared to size of the destination.
So While debugging Buffer overflow occurs.
We choose alternative Function like memcpy which perform Bound checking.We can consider strncpy() also.
Memcpy:
The Syntax of Memcpy is
void *memcpy(void*dest,const void* src,int n)
dest-destination buffer.
src-source buffer.
In that n defines the number of bytes to be transferred.
Example:
#include <stdio.h>
#include <string.h>
int main()
{
char buf[4]= "";
char sun[12]= "how are you";
memcpy(buf, sun, 4);
printf("%s\n", buf);
return 0;
}
In the above program only 4 bytes of data can be transfered to buf from sun.
So with the help of this memcpy we can avoid buffer overflow.
But the disadvantage of memcpy is when overlapping between source and destination occurs
means it also made buffer overflow while debugging.It does n't copy the data safely before overlapping.
That problem can be solved with the help of memmove function.
The other difference between strcpy and memcpy is explained below the program:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[10] = "abc\0def";
char s2[10] = "";
char m1[10] = "abc\0def";
char m2[10] = "";
int i;
strcpy(s2,s1);
for(i=0;i<10;i++)
{
printf("STRCPY %d= %c\n",i,s2[i]);
}
memcpy(m2,m1,10);
for(i = 0; i <10; i++)
{
printf("MEMCPY %d - %c\n", i, m2[i]);
}
return 0;
}
The Output of the above program is:
STRCPY 0= a
STRCPY 1= b
STRCPY 2= c
STRCPY 3=
STRCPY 4=
STRCPY 5=
STRCPY 6=
STRCPY 7=
STRCPY 8=
STRCPY 9=
MEMCPY 0 - a
MEMCPY 1 - b
MEMCPY 2 - c
MEMCPY 3 -
MEMCPY 4 - d
MEMCPY 5 - e
MEMCPY 6 - f
MEMCPY 7 -
MEMCPY 8 -
MEMCPY 9 -
So in the above result the function strcpy copy the contents until it finds '\0'(NULL)character.
But memcpy copies the content without considering the NULL termination.
Strcpy with integers:
#include<stdio.h>
#include<string.h>
int main()
{
int dst[3]={60,90,50};
int src[3]={30,25,35};
strcpy((char *)dst,(char *)src);
for(int i=0;i<3;i++0
{
printf("dst=%d\n", dst[i]);
}
}
The output is:
dst=30
dst=90
dst=50
Even we use type casting for integers it won't workout in strcpy.Because size of the integers and charcters are different.
So The other thing which makes variation between strcpy()and memcpy() is, memcpy() is adaptable for both characters and strings.
strcpy() is adaptable only for strings.
No comments:
Post a Comment