Access to an access array outside of C and C ++

int data[8]; data[9] = 1; 

What does the C ++ standard say about this? Is this behavior undefined?

At least the C compiler (gcc -std = c99 -pedantic -W -Wall) says nothing about this.

Thanks.

+4
source share
7 answers

Access to the outer borders of the array is undefined behavior, from c99 draft standard in the Annex J.2 J.2 undefined behavior the following item is included:

The array index is out of range, even if the object is apparently accessible using (as in the expression lvalue a [1] [7], given the declaration int a [4] [5]) (6.5.6).

and the draft C ++ standard in section 5.7 Additive operators in paragraph 5 say:

When an expression that has an integral type is added or subtracted from the pointer, the result is the type of the operand of the pointer. If the pointer operand points to an element of the array object and the array is large enough , the result indicates the offset of the element from the original element, so that the difference between the indices of the resulting and original elements of the array is equal to the integral expression. [...] If both pointer operands and the result point to elements of the same array object or one after the last element of the array object, the evaluation should not lead to overflow; otherwise, the behavior is undefined.

For completeness, Section 5.2.1 Subheading 1 states:

[...] The expression E1 [E2] is identical (by definition): * ((E1) + (E2)) [Note: see 5.3 and 5.7 for more details of * and + and 8.3.4 for details information about arrays. -end note]

It is important to note that the compiler does not need to create a warning (diagnostic) for undefined behavior, the draft C ++ standard in section 1.4 Implementation Compliance Parameter 1 reads:

The set of diagnosed rules consists of all syntactic and semantic rules in this International Standard , with the exception of those rules that explicitly indicate that "no diagnostics are required" or which are described as the result of "undefined."

+6
source

Yes, this behavior is undefined.

The compiler may or may not warn you about undefined behavior, even if it is able to detect it.

+4
source

This is considered undefined. Compilers are not required to issue warnings if you are trying to compile code that will lead to undefined behavior, although they are pleased.

Hope this helps!

+4
source

Undefined. This may or may not be an invalid memory, which makes it dangerous. You can use tools like valgrind to detect bad accesses like this.

+2
source

Yes, this behavior is undefined. Everything can happen, it can work or not, it can work 2 years, and then stop working. This is the most dangerous of the three:

  • undefined behavior
  • unspecified behavior
  • implementation-defined behavior

You can check this to meet your other relatives: What is all the usual undefined behavior that a C ++ programmer should know about?

Undefined, undefined and implementation-defined behavior

+2
source

C and C ++ do not check boundaries. The values ​​you are trying to achieve can be almost anything. It may work on your compiler, but it is not legal C or C ++, and there is no guarantee that it will work the next time the program starts.

In accordance with ISO C, access to an array of external delimiters causes

undefined behavior: when using an intolerable or erroneous software construct or erroneous data for which this International Standard does not impose any requirements

Segmentation errors occur when you try to dereference a pointer to a memory that your program is not allowed to access, and just walking past the end of your array will probably not cause this. But this is likely to give you some bad values.

+2
source

Yes, this behavior is undefined, some compilers give warnings about this, others do not, but let's see what your code does.

Take a look at the built-in implementation of otators [] . a[b] is actually *(a + b) . So go back to your code.

 int data[8]; data[9] = 1; 

First, you select a portion of the stack and create a pointer to the first element. Then you overwrite some data that is immediately after your array, so you will corrupt some data.

Take another example:

 int data[8]; int data2[8] = {}; data[9] = 1; 

It is very likely that the compiler generates code that selects once and creates two pointers in the form of arrays. Thus, data[9] = 1; can set the second value of data2 to one, but there is no guarantee about it.

+1
source

Source: https://habr.com/ru/post/1501541/


All Articles