Array subscript out of bounds and memory overflow

Source: Internet
Author: User

Very similar two concepts, inadvertently will be confused

First, make a general explanation of the two nouns: subscript out of bounds
When referencing an array element, the subscript used exceeds the scope of the array subscript, but it should be noted that:
C + + does not check the array for bounds. You can rewrite each end of the array and write an array of other variables or even code that writes to the program. It can improve the efficiency of program running without checking whether subscript is out of bounds. Because if you check, then the compiler must add additional code in the generated target code for the program runtime to detect whether the subscript is out of bounds, which will cause the program to run down, so in order to run the efficiency of the program, C + + Do not check whether the subscript is out of bounds. If an array subscript crosses the line, it will automatically then write back the block of memory.
For information on why C + + does not check the bounds of the array, you can refer to:
Http://www.xuebuyuan.com/967089.html
Because the compiler does not automatically detect whether your array subscripts are out of bounds, but instead gives the task to the programmer, we must be careful not to let the subscripts of the array cross over while we write the program and refer to the array elements.
Also, beginners must not forget that the subscript of the array is starting from 0, not the common sense from the beginning of the 1. Memory overflow
When an array is initialized ( assigning values to an array element), the number of initialization (Assignment) elements exceeds the number of elements when the array is defined. The number of elements here is the number in the square box when the array is defined, and for multidimensional arrays, the number of elements = the product of numbers in each square box.
Of course, the number of array elements can be calculated by using the formula:
Number of array elements = sizeof (array name)/sizeof (array any element)

Use two specific examples to see the difference between them:

1. Subscript out of Bounds

#include <stdio.h>

int main ()
{
    int i=10, arr[10];
    int sz = sizeof (arr)/sizeof (arr[0));
    int index = 0;
    scanf ("%d", &index);
    Arr[index] =;
    printf ("arr[%d] =%d\n", Index,arr[index]);
    System ("pause");
    return 0;
}

The array is defined as arr[10] and its subscript range is 0–9, and the subscript overflow occurs beyond this range


The above two pictures are in the VS2013 inside of the operation result, two input of the subscript will be crossed, but why the first one to repay the wrong (array subscript out of bounds), and the second one did not.
This is because VS2013 this compiler when processing array subscripts, only think that when the subscript is equal to the number of elements of the array, the mark is out of bounds (may be afraid of beginners to the array as a 0 start bar), other circumstances do not go back to detection and processing (when I entered the value is greater than or equal to 11, the program will not complain)


The above is in the VC6.0 of the operation results, VS2013 different, VC6.0 error occurred in the input value of 11 o'clock (when the input 12 o'clock will also be an error), and the input 10 o'clock does not complain.
This is because VC6.0 that when referencing an array element, if the subscript of the array is larger than 1 (or greater than 2) of the elements of the array, the label is crossed. The rest of the situation is not detected.

And in the Linux (CentOS6.5) running, is that we start to say the kind of subscript out of bounds, no matter how you cross the line, my compiler will not be labeled as a boundary check.

2, Memory overflow

#define _crt_secure_no_warnings 1

#include <stdio.h>          

int main ()
{
    int count[100], I;
    for (i = 0; I <= i++)
    {
        count[i] = 1;
    }
    System ("pause");
    return 0;
}

Array is defined as count[100], there are 100 elements in the array. When initialized, the For loop is cycled 101 times, so the value of 101 elements is initialized to the array, and the array has only 100 elements, which results in a memory overflow.

VS2013 Environment, the compilation link can run, but it throws an error->> array memory overflow.

This is true in the VC6.0 environment, the compilation link can run, but the same can be wrong.

This is in the Linux (CentOS6.5) environment, the compilation link can be run, but the program can not stop, close the terminal to see the program is still in progress.

Therefore, although the array in C language is a very powerful thing, but in the application must pay attention to, do not appear subscript out of bounds, because this will cause unimaginable errors. For example, in the case of the subscript out of bounds, I am defined as 10, when in the VS2013 environment, I entered the subscript value of 12, in the output ARR[12] = 20, while the value of I will be changed to the two variables defined in VS, the allocation of memory in two variables in the memory space between the two space, The size of the space is the size of the shape), so that I can be used elsewhere to get the wrong value of I, which may lead to a series of errors, resulting in a far cry from the expected results. Also, when initializing an array, be careful not to initialize the number of elements, not exceeding the number of definitions.

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.