Today, when I hit the code to find a bug and everyone to share, I hope we do not want to be the same as my mistake next time.
If you make the same mistake as me, you can know where you are wrong! < (^-^) >
The function is as follows: (Fibonacci sequence implementation)
Long long fibonacciseq (int n) {Long long *fib=new long 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 long ret = Fib[n];d elete[] fib;return ret;}
If the argument passed to the function is 0, the program crashes because it triggers a breakpoint.
Single-step debugging, according to the memory window will find the problem!
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/F5/wKiom1cN7vbSp95ZAAAd0RbnsO0357.png "style=" float: none; "title=" QQ picture 20160413150253.png "alt=" Wkiom1cn7vbsp95zaaad0rbnso0357.png "/>
After the memory initialization of the FIB point is complete, its memory condition is as
We can observe that there are FD FD FD FD in front and back of this memory,
0xFD to initialize protected memory (Debug version adds protected memory before and after dynamically allocating memory to prevent cross-border access)
and fib[1] = 1; The FD FD fd and the four bytes behind it are changed to 01 00 00 00 00 00 00 00, as follows:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/F2/wKioL1cN76zj--DFAAAW0xjxbs4749.png "style=" float: none; "title=" QQ picture 20160413150310.png "alt=" Wkiol1cn76zj--dfaaaw0xjxbs4749.png "/>
The program runs to delete[] fib; only the start of "protected memory flag" is found, and another "protected memory flag" cannot be found, so the program will fail!
In fact, it is very simple, that is, a cross-border visit! I only new one space, but saved two variables, of course not!
This article from "Yan Anyang" blog, declined reproduced!
Cross-border access to new