一个数组越界赋值的例子(很形象很生动)

char string[1]; // string[10] is the boundary example to tell the extra '0' in a c style string will also be copied when copying.
char *str1="0123456789";
strcpy(string, str1);  // 11 char will be copied to the destination memory.

Running above sample code, you will see that string array memory area will be copied with "0123456789" & ‘0’ as the ending of c style string although in fact string array is not long enough to contain whole string in str1.  Finally, later it will cause "stack corruption" error and the program would stop running.
一个数组越界赋值的例子(很形象很生动)_第1张图片

你可能感兴趣的:(数组)