Static, Cross-border, and overflow __c languages for C-language arrays

Source: Internet
Author: User
Tags array length

C Learning Network: Link->C Learning Network

C Training Network: Link->C Training Network in C, once the array is defined, the memory space occupied is fixed, cannot insert elements at any location, and can not delete elements anywhere (of course, you can modify elements), we call such an array (static array). array out of bounds The C language array does not automatically expand, and when the current is less than 0 or greater than or equal to the length of the array, there is a cross border (out of Bounds) that accesses memory other than the array. If the subscript is less than 0, the lower bounds will occur (off normal Lower), and if the subscript is greater than or equal to the length of the array, the upper bounds will occur (off normal Upper).

C language in order to improve efficiency, and do not check the cross-border behavior, even if the line is out of bounds, can be compiled, the problem can only occur during the run. Take a look at the following code:

#include <stdio.h> #include <stdlib.h> int main () {int a[3] = {Ten,}, I; for (i=-2; i<=4; i++) {print F ("a[%d]=%d\n", I, a[i]); System ("pause"); return 0; Run Results:
a[-2]=-858993460
a[-1]=-858993460
a[0]=10
A[1]=20
A[2]=30
a[3]=-858993460
a[4]=-858993460

The array elements that are accessed across borders are garbage values that have no real meaning because we don't know what is in the memory outside of the array, it may be the value of other variables, possibly additional data, possibly an address, which is not controllable.

Because of the "laissez-faire" of the C language, we must be very careful when accessing arrays to ensure that there is no crossing.

When an array crosses over, if we have access to it, the program will run normally, but the result will be uncontrollable (as shown in the previous example), and if there is no access, the program will crash. Take a look at the following example:
#include <stdio.h> #include <stdlib.h> int main () {int a[3]; printf ("%d", a[10000]); System ("pause"); 0; Running under C-free 5.0 pops up a dialog box that the program stops working:


Running under VS2010, a run-time error occurs:


Each program can access memory is limited, the program to access the memory of the 4*10000 byte, obviously too far, beyond the scope of the program access. This place memory may be other program's memory, may be the system itself occupies the memory, possibly is the unused memory, if lets this behavior, will have the very dangerous consequence, the operating system can only cause the program to stop running. Array OverflowOverflow (Overflow) occurs when the number of elements assigned to an array exceeds the length of the array. As shown below:
int a[3] = {1, 2, 3, 4, 5};
The array length is 3, but the initialization gives 5 elements, exceeding the array capacity, so that only the first 3 elements can be saved, and the following elements are discarded.

Vc/vs found that an array overflow would be an error, preventing compilation from being passed, and GCC would not, it would only give a warning.

In general, array overflow is not a problem, the most is to discard the extra elements. However, for a character array, sometimes an uncontrollable situation arises, see the following code:
#include <stdio.h> #include <stdlib.h> int main () {char str[10] = "http://c.biancheng.net"; puts (str); system ("pause"); return 0; Possible results of the operation:


The length of the string is greater than the length of the array, which can only hold the previous part of the string, that is, "http://c.b", even if the compiler at the end of the "" ", it can not be saved in the array, so printf () scan the array does not encounter a Terminator '" ", The data in the back of the memory we do not know what is, the character can be recognized, when encountered ' ", these are uncertain. When the character is not recognized, there will be garbled, displaying strange characters.

Thus, when assigning values to a character array in a string, the length of the array is greater than the length of the string to accommodate the Terminator ' ".
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.