Submitter:Fred Tydeman
Submission Date: 2019-11-04
Document: WG14 N2447
Title:N2447: Missing example in 6.5.6
Reference Documents:

Summary

As part of the Memory Object Model presentation (SC22WG14.17229, slides), it was stated that there is an example in the C Standard about multi-dimensional arrays. In trying to find such an example, all that I found was:

J.2 has:

- 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).

Yet, if one looks in 6.5.6, there is no text or example about that issue.

After doing some research, that example is missing from C17, C11, C99, C95, and C89. The text for J.2 came from C89, Defect 17, question 16. The committee's answer to that question did have an example (which was not added to the standard) along with the above words that are in J.2 (was G.2 in C89).

Committee response in that old DR against C89:

Response

For an array of arrays, the permitted pointer arithmetic in subclause 6.3.6, page 47, lines 12-40 is to be understood by interpreting the use of the word "object" as denoting the specific object determined directly by the pointer's type and value, not other objects related to that one by contiguity. Therefore, if an expression exceeds these permissions, the behavior is undefined. For example, the following code has undefined behavior:


   int a[4][5]; 
   a[1][7] = 0; /* undefined */
 

Some conforming implementations may choose to diagnose an "array bounds violation," while others may choose to interpret such attempted accesses successfully with the "obvious" extended semantics.

Correction

Add to subclause G.2, page 201:

- 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).

Based upon the above, I believe that the example should be added to C2X. In addition, words about "objects related by contiguity" should be added as they may impact the memory model.

Suggested Technical Corrigendum:

Add to §6.5 Expressions a new paragraph after paragraph 6 (about effective type):

For an array of arrays, the permitted pointer arithmetic is to be understood by interpreting the use of the word "object" as denoting the specific object determined directly by the pointer's type and value, not other objects related to that one by contiguity; hence a slice of an array is not an independent object. Therefore, if an expression exceeds these permissions, the behavior is undefined.

EXAMPLE


   int a[4][5]; 
   a[1][7] = 0; /* undefined */
 

Suggested Practice:

Conforming implementations should diagnose an "array bounds violation."

Change the existing referenced J.2 item to refer to 6.5 instead of 6.5.6