New cross-border access, new cross-border access

Source: Internet
Author: User

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!

Related Article

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.