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

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

At least the C compiler (gcc -std=c99 -pedantic -W -Wall) doesn't say anything about it.

3
  • 8
    In fact, even data[8] is undefined behavior.
    – Dan
    Sep 10, 2013 at 19:14
  • 3
    As a side note: data[8] = anything is also undefined behavior since your array would end at data[7]. Sep 10, 2013 at 19:16
  • Do you have any questions about the answer to this? Jul 16, 2014 at 9:43

7 Answers 7

8

Accessing outside the array bounds is undefined behavior, from the c99 draft standard section Annex J.2 J.2 Undefined behavior includes the follow point:

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

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

When an expression that has integral type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integral expression. [...] If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.

For completeness sake, section 5.2.1 Subscripting paragraph 1 says:

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

It is important to note that the compiler is not required to produce a warning(diagnostic) for undefined behavior, the draft C++ standard in section 1.4 Implementation compliance paragraph 1 says:

The set of diagnosable rules consists of all syntactic and semantic rules in this International Standard except for those rules containing an explicit notation that “no diagnostic is required” or which are described as resulting in “undefined behavior.”

4

Yes, it is undefined behavior.

A compiler may or may not warn you against undefined behavior even if it is able to detect it.

4

This is considered undefined behavior. Compilers aren't required to issue warnings if you try to compile code that will result in undefined behavior, though it's nice of them to do so.

1
  • 1
    I think a lot of behavior is marked as "undefined" because it's really hard for compilers to find these errors. Sep 10, 2013 at 19:39
3

C and C++ don't check bounds. The values that you are trying to reach could be practically anything. It might seem to work on your compiler, but it is not legal C or C++, and there is no guarantee that it'll still work the next time you run the program.

According to the ISO C Standard, accessing an array outside bounders causes

undefined behavior: behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

Segmentation faults occur when you are trying to dereference a pointer to memory that your program is not allowed to access, and just going past the end of your array probably won't cause that. But it will most-likely give you some bad values.

2

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

2

Yes, it is undefined behavior. Everything might happen, it might work or not, it might work 2 years and then stop to work. This is most dangerous out of three:

  • undefined behavior
  • unspecified behavior
  • implementation-defined behavior

You might check this to meet other its relatives: What are all the common undefined behaviour that a C++ programmer should know about?

Undefined, unspecified and implementation-defined behavior

1

Yes, it is undefined behavior, some compilers gives warnings about that, others don't, but lets look, that your code do.

Look at opeators [] inline implementation. a[b] actually is *(a + b). So go back to your code.

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

First you allocate some part of stack and create a pointer to the first element. Then you rewrite some data, which is just after your array, so you corrupt some data.

Lets take another example:

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

It is very likely, that compiler generates code which allocates once and creates two pointers as arrays. So data[9] = 1; may set second value of data2 to one, however there is no guarantees about that.

Not the answer you're looking for? Browse other questions tagged or ask your own question.