Please refer to the following smallProgram--- A lot of attacks
# Include <stdio. h> # define Max 255int main () {unsigned char a [Max], I; for (I = 0; I <= max; I ++) A [I] = I; for (I = 0; I <= max; I ++) printf ("% d", a [I]); printf ("\ n"); Return 0 ;}
It seems that there is no problem. Let's take a closer look and find that the array is out of bounds. But let's take a closer look at it...
When I = 255, You can execute a [255] = 255. Because the array does not check whether the access to the array element is out of bounds, and then I ++, how much does I become? Note that this is unsigned. I + 1 will become 11111111 + 1 = 100000000. The highest bit is removed, then I = 0; then it becomes an endless loop. So there is no problem in compiling, but there is a problem in running.
Let's look at a similar example.
# Include <stdio. h> int main () {char K = 0; while (k> = 0) K ++; printf ("% d \ n", k); Return 0 ;}
The answer is-128, which is similar to the one above. After K is converted to the ASCII code value, K ++ runs until 11111111, and then adds 1, computer computing addition is the complement of computing, so the 100000000 high is removed, it is 00000000, then the complement, 11111111, and then this is-128. different from the one above, this is not unsigned, so there is a negative number.
I found that programming is never proficient without knowing the underlying operating mechanism.