-1

I'm trying to take the difference of the two rows using the for loop. There are total four rows, so the result would be simply 3 numbers after subtraction and summations. But the loop provide additional results which are unreasonable!! Does anyone know why it happens?

#include <iostream>
#include <vector>

using namespace std;

int main()
{   
    int v[4][3] = { 
        {1,2,3}, 
        {3,4,5}, 
        {6,7,8}, 
        {9,10,11} };
    cout << sizeof(v) << "\n";
    float sq = 0;
    for(int i = 0; i < 3; ++i){
        for(int j = 0; j < 2; ++j){
                sq += (v[i+1][j] - v[i][j])  + (v[i+1][j+1] - v[i][j+1]) + (v[i+1][j+2] - v[i][j+2]);
                cout << "diff " << sq << endl;
        }
    }
    cout << "final square of the numbers: " << sq << endl;
    return 0; 

}
``
Mahesh
  • 556
  • 1
  • 3
  • 16
  • 7
    You are accessing elements out of bounds so you are invoking undefined behavior. – erikkallen Nov 03 '21 at 20:00
  • 1
    Hint: What is the index `(j + 1)` when `j == 3`? – Thomas Matthews Nov 03 '21 at 20:01
  • 5
    Your `j` loop range is too big and you don't check for `i+1`, `j+1`, or `j + 2` being out of bounds. – crashmstr Nov 03 '21 at 20:01
  • 1
    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) – Code-Apprentice Nov 03 '21 at 20:06
  • _"Does anyone know why it happens?"_ It's undefined behavior, nobody can actually tell, what exactly will happen. Well, indices are fixed from zero to array size - 1, and your indexing variables go out of boundaries?? I have no idea, why a question about a more or less _typo_ gets that many upvotes and useless answers – πάντα ῥεῖ Nov 03 '21 at 20:07
  • If you had actually used `std::vector` (you #included it), and used `at()` instead of `[ ]` to access the elements, you would have answered your own question. A `std::out_of_range` exception would have been thrown, and you would not see any "unreasonable results" because your program would have been terminated. [See this](http://coliru.stacked-crooked.com/a/4071764bdb510bef) – PaulMcKenzie Nov 03 '21 at 20:11
  • @PaulMcKenzie Well said. `std::vector` or... `std::array` – MatG Nov 03 '21 at 20:51

1 Answers1

3

Access to v at or past 4 in the first index or 3 in the second is undefined behavior.

If you are lucky, you get nonsense. If you are luckier, your program crashes when you test it. If you are unlucky, the program does any arbitrary action within its power.

In your case, you access up to v[4][5].

A programming executing undefined behavior has no constraints put on it by the C++ language.

When i=3, j=3, we get:

sq += (v[4][3] - v[3][3])  + (v[4][4] - v[3][4]) + (v[4][5] - v[3][5]);

and all of those access to v are illegal as they are past the array bounds in either the first or second dimension, or both.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • I tried make the smaller bounds for e.g., *i = 3; i < 3* and *j = 2; j <2*, but still getting one value which does not make sense.. – Mahesh Nov 03 '21 at 20:13
  • 1
    @Rohit -- See my comment. Actually use `std::vector` instead of just including `` it. You will then see the issues plainly, because your program would stop dead (if you use `at()`). – PaulMcKenzie Nov 03 '21 at 20:16
  • @Rohit I am unable to read your mind. So I don't know what you want your code to do. I can look at what your code does, which is undefined behavior. Sadly, being able to describe what you want your code to do is a hard problem; doing so is more than half the challenge to being able to program it. So I cannot help you. – Yakk - Adam Nevraumont Nov 03 '21 at 20:38