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

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.

posted @ 2008-07-01 15:22  能巴  阅读(833)  评论(0编辑  收藏  举报