Be careful about array size when u are using strcp

This tip submitted by Bhushan Verma on 2005-10-19 03:17:26. It has been viewed 46359 times.
Rating of 5.3 with 232 votes

Array/Buffer Overflow

If we copy or insert data more into an array of limited size, it is referred as array overflow.
Look at the following code:

char var1[10];
char var2[7] = \"bhushan\"; /* '\0' is not added ,as size is given as 7*/
strcpy( var1, var2 );

Here, we can find that var2 (“bhushan”) is not terminated with a Null terminator (‘\0’). So when we copy var2 to var1 using strcpy( ), the strcpy( ) routine will copy all the character to var2 until it finds ‘\0’ in memory. So array overflow may result in memory overwrite!

More tips

Help your fellow programmers! Add a tip!