4

From this reference, In C it seems the following behavior is undefined.

int my_array[100][50];
int *p = my_array[0];
p[50]; // UB

Is there a reference in C++03 or C++11 which confirms this?

2501
  • 25,460
  • 4
  • 47
  • 87
cpx
  • 17,009
  • 20
  • 87
  • 142
  • Why do you think that `p[50]` involves undefined behaviour? – Šimon Tóth Dec 08 '11 at 09:27
  • Funny, I can compile your example with `gcc -Wall -Wextra` and get no warnings about any undefined behavior; change `p[50]` to `p[50] = 1;` to quiet the "no effect" warning and make sure it knows it should generate the code. (Add in some `printf(3)` calls if you want to make sure it doesn't optimize away the line.) – sarnold Dec 08 '11 at 09:28
  • @Let_Me_Be: read the link. Seems to be asking about two different things and it needs to be clarified whether he wants to know about 2D array pointers (as per the link) or static arrays (as per his code). – tinman Dec 08 '11 at 09:28
  • @Let_Me_Be: Please see the link I added in question. – cpx Dec 08 '11 at 09:32
  • @tinman I would really like to see the mentioned official interpretation. Arrays are guaranteed to be sequential even in C89/90. So unless array is an array of pointers, it will be well defined. – Šimon Tóth Dec 08 '11 at 09:33
  • @let that an arbitrary address computation ends up being the address of a valid object is not sufficient for it being dereferencable. Address computations can only be done in limited ways to help the compilers optimizer. – Johannes Schaub - litb Dec 08 '11 at 09:41
  • @JohannesSchaub-litb Yes, that is true, but while it stays within the memory boundaries of the original object, it should all be well defined. – Šimon Tóth Dec 08 '11 at 09:42
  • 1
    @let it does not stay within the elements of the int[50] array. – Johannes Schaub - litb Dec 08 '11 at 09:44
  • @JohannesSchaub-litb But the `int[50]` array isn't a separate entity. – Šimon Tóth Dec 08 '11 at 09:47
  • possible duplicate of [May I treat a 2D array as a contiguous 1D array?](http://stackoverflow.com/questions/7269099/may-i-treat-a-2d-array-as-a-contiguous-1d-array) – fredoverflow Dec 08 '11 at 09:49
  • 3
    It is an own entity. Subobjects are objects and objects are an entity kind (in c++). Im not aware of the notion "entity" in the context of C. – Johannes Schaub - litb Dec 08 '11 at 09:55

2 Answers2

6

Yes in the description of the + operator. You may not dereference that pointer in C because it is a past the end pointer for the first subarray. In C++ this currently is legal because the pointer points to a valid integer (the points to relation is defined somewhere in clause 3). However in both standards adding more than 50 yields undefined behavior.

A DR was recently sent to the c++ committee about the rule that dereferencing such "valid out of thin air" pointers may be dereferenced, so i would not rely on that.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • Damn, I don't have C89 standard here, but C99 talks about a _pointer that points to an array_ in a case of the multidimensional array, I would personally interpret this as define behaviour since the pointer doesn't point outside of the original array, only outside of the sub-array. – Šimon Tóth Dec 08 '11 at 09:41
  • To clarify, when you say "Yes", you mean "Yes the behaviour is undefined"; (the question title is the negative of the question in the actual post!). And the standard section that says you can't add more than 50 is `[expr.add]#5`. – M.M Jun 19 '14 at 23:22
-1
int my_array[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
for(i=0;i<3*3;i++)
{
    printf("%d,",*(*my_array+i));
}

output is 1,2,3,4,5,6,7,8,9,

I think you can do it like that.

MugosDynamic
  • 99
  • 1
  • 7