Beware of array out-of-bounds!

Source: Internet
Author: User

For the first time today, I felt deeply about the array out-of-bounds problem. If the array out-of-bounds is run in the release version, no error is reported. If it is a debug version, if you reference the array subscript directly by a constant value, the array is detected to be out of bounds. If you reference the array subscript through a variable, the array subscript will not be detected !!!

Test the following code,

1 # include <stdio. h> 2 3 int main (void) 4 {5 Int arr [1] [1] = {20}, BRR [1] = {1}, y = 5; 6 7 // printf ("% d \ n", arr [5] [0] = 0); // In the debug version, directly referencing the array subscript with constants will be detected by vs2010 when the array is out of bounds 8 printf ("% d \ n", arr [y] [0] = 10 ); // No error is reported in the debug version. If the array subscript is referenced by a variable, it will not be detected if the subscript is out of the range !!! 9 // printf ("% d \ n", BRR [5] = 0); // In the debug version, directly referencing the array subscript with a constant will be detected by vs2010 that the array is out of bounds 10 printf ("% d \ n", BRR [y]); // No error is reported in the debug version, if the array subscript is referenced by a variable, it will not be detected if it is out of the range !!! 11 12 Return 0; 13}

Run in the release version. The arrays in the four printf versions are out of bounds, but vs2010 directly releases them.

At this time, vs2010 will not detect cross-border issues!

Run the following command in debug:

The test results of the debug version are also very small, and the array subscript can be referenced as a variable to allow direct access:

It seems that you should always pay attention to this problem in your mind. Otherwise, when the array crosses the border, you can only get dizzy when looking at the inexplicable output of the program!

Beware of array out-of-bounds!

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.