New cross-border access, new cross-border access
I found a BUG when I typed the code today. I hope you will not make the same mistake next time.
If you make the same mistake as me, you can also know where your mistake is! <(^-^)>
The function is as follows: (implementation of the Fibonacci series)
Long FibonacciSeq (int n) {long * fib = new long [n + 1]; fib [0] = 0; fib [1] = 1; for (int I = 2; I <= n; I ++) {fib [I] = fib [I-1] + fib [I-2];} long ret = fib [n]; delete [] fib; return ret ;}
The above program has a BUG. If the parameter passed to the function is 0, the program will crash because of triggering a breakpoint.
Single-step debugging: the problem will be detected based on the memory window!
After the memory class pointed to by fib is initialized, its memory is shown in figure. We can see that there is fd before and after the memory class, 0xFD is used to initialize the protected memory (debug adds the protected memory before and after the dynamically allocated memory to prevent cross-border access), while fib [1] = 1; change fd and the four bytes following it to 01 00 00 00 00 00 00, for example:
When the program runs to delete [] fib, only the starting position of the "protected memory mark" is found, and the other "protected memory mark" cannot be found, so the program will fail to run!
In fact, it is very simple, that is, cross-border access! I only added a new space, but saved two variables. Of course not!