1

Here is my code,

int a[3][3] ={
            {0,1,2},
            {3,4,5},
            {6,7,8}
    };
for(int i = 0 ; i < 3 ; i ++)
        for(int j = 3 ; j>0 ; j--)
            cout << a[i-1][j] << " ";

I expected the output to be "0 0 0 0 2 1 0 5 4"
but it turns out to be "0 0 1 3 2 1 6 5 4"

What is the reason behind this?

Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
jyann
  • 13
  • 3
  • 2
    What is the logic you have in mind to get `0 0 0 0 2 1 0 5 4` as output, for the given input? – t.niese Dec 28 '19 at 08:00
  • 1
    Did you intend `cout << a[i][j-1] << " ";`? That wouldn't give you the output you want, but would prevent invoking *Undefined Behavior*. – David C. Rankin Dec 28 '19 at 08:01
  • @t.niese the way I understood it, it is right to left and top to bottom. Add a zero row at the top. – Empty Space Dec 28 '19 at 08:03
  • @t.niese the logic behind that is i assume undefined/uninitialized value as 0 – jyann Dec 28 '19 at 08:03
  • 1
    In your `a[3][3]` array you have `9` values, so there are no undefined/uninitialized values in that array. And anything that is out of the bounds of that array can be anything. – t.niese Dec 28 '19 at 08:06
  • Does this answer your question? [Accessing an array out of bounds gives no error, why?](https://stackoverflow.com/questions/1239938/accessing-an-array-out-of-bounds-gives-no-error-why) – Jaideep Shekhar Dec 28 '19 at 08:17

4 Answers4

6

What is the reason behind this?

First of all, out of bounds array access is undefined behavior. So your first access to a which is a[-1][3] is not valid.

But to your question what is happening there:

You can imagine your:

int a[3][3] = {
  {0, 1, 2}, 
  {3, 4, 5},
  {6, 7, 8}
};

to have this layout in your system memory:

+---+---+---+---+---+---+---+---+---+
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
+---+---+---+---+---+---+---+---+---+

Your [-1] in a[-1][3] points three elements before your arr array.

 arr[-1]     arr[0]      arr[1]
  ↓           ↓           ↓
+---+---+---+---+---+---+---+---+---+---+---+---+
| ? | ? | ? | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
+---+---+---+---+---+---+---+---+---+---+---+---+

With [3] in a[-1][3] you try to access the element at index 3 from that position.

  0   1   2   3
+---+---+---+---+---+---+---+---+---+---+---+---+
| ? | ? | ? | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
+---+---+---+---+---+---+---+---+---+---+---+---+

So the first value you see is the first element of your arr, the next two a[-1][2] and a[-1][1] are "garbage" values.

That's what happens on your system, but keep in mind that this relies on undefined behavior.

t.niese
  • 39,256
  • 9
  • 74
  • 101
  • I believe that this question does not add anything over [this one](https://stackoverflow.com/questions/1239938/accessing-an-array-out-of-bounds-gives-no-error-why). Also, you might want to link to [this post](https://stackoverflow.com/a/12653518/9414470). Still, +1. – Jaideep Shekhar Dec 28 '19 at 08:46
3

See first 4 and 7th value in the output( 0 0 1 3 2 1 6 5 4 ) you are getting are garbage value and they can be anything. When i executed this code i'm getting 0 2 0 3 2 1 6 5 4. It is because you are accessing out of bounds values for a[-1][3], a[-1][2], a[-1][1], a[0][3] and a[1][3].

Himanshu Singh
  • 2,117
  • 1
  • 5
  • 15
2

I see a bug in

for(int j = 3 ; j>0 ; j--)

j should start from 2 and not 3 as the indexing is 0 based. The condition should be j >= 0.

This should look like for(int j = 2 ; j >= 0 ; j--).

Or you could go for inner loop like

for(int j = 3 ; j - 1 >= 0 ; j--)
        cout << a[i-1][j-1] << " ";

But "0 0 0 0 2 1 0 5 4" doesn't make sense to me with the kind of looping you are trying.

Shridhar R Kulkarni
  • 6,653
  • 3
  • 37
  • 57
1

in the first loop, i is equal to 0 and when you use

cout << a[i-1][j] << " ";

i-1 is -1 and I think this is your problem because accessing the -1 index of the array will cause undefined behavior