Cross-border access to new

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.