About the debug error:damage before normal block

Source: Internet
Author: User

At the end of the program run, VC + + 6.0 If such a dialog box appears



is to trigger the debug error:damage before/after normal block.

If the compiler reports such a mistake, it is almost certainly in the program that the array accesses the bounds. For example:

int main ()

{

int *p=new int[5];

p[-1]=3;

delete [] p;

return 0;

}

When you execute to the DELETE statement, the above dialog box pops up, reporting damage before normal block

If the program is modified like this

int main ()

{

int *p=new int[5];

p[5]=3;

delete [] p;

return 0;

}

When you execute to the DELETE statement, you report an error damage after the normal block.

What is normal block? When you use the new operator to request memory space for a pointer, the memory space that the VC compiler assigns to the pointer is normal block. The initial value of each byte in this memory space is CD H. And put the memory space before 4 bytes and then 4 bytes

The initial value of the assignment is FDFDFDFD H.

The VC memory tool makes it easy to see the contents of this newly allocated memory space. As above two programs, VC for pointer p to request the memory space address from 0x00032f48 start. Use memory to view the content near this address, which is what the following figure shows:


When the memory space is freed with delete, the compiler checks that the 4-byte contents and the 4-byte contents of the memory are changed before that segment. If so, report damage before normal block and damage after normal block respectively.

However, the VC compiler of this error-checking function is very limited to prevent array access out of bounds, mainly relying on the programmer to be careful. As above the

In a program, the compiler cannot detect such an error if you let p[-2]=3, that is, change the memory before the memory address 0x00032f44. Similarly, P[6] is assigned a value, or will be P[5]=0XFDFDFDFD, the compiler will not error.

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.